Reputation: 1
((lambda (proc) (proc "yes" "no")) (lambda (a b) a)) ;; yes
((lambda (a b) a) "yes" "no") ;; yes
My question is why are 1 and 2 has the same result and are they the same? Can someone please elaborate it a bit clearly ?
Upvotes: 0
Views: 39
Reputation: 29021
They give the same result, but they're not "the same". They are two language constructs that happen to give the same result (well, yes, in a functional world, they are equivalent, but I don't think you're refering to that.)
Really, understanding the constructs goes down understanding the language itself (Scheme). I can give you a some hints.
Roughly speaking, the construction (X Y)
means "function application". That is, the first X
is a function and Y
is a parameter, or a set of them. Then the function X
is called with the Y
parameter(s).
Take your second line, for example, you can see, decomposing it, that it has:
(<Function> "yes" "no")
that is, something that may be a function, then two parameters. In Scheme, you can build anonymous functions, that have no name, but that you specify what do they expect as parameters and what do they do when called. This is called a "lambda function". In your case, the lambda function is defined as:
(lambda (a b) a)
That is, a function without name that accepts two parameters a
and b
and returns the first one. If you put all together, you get
((lambda (a b) a) "yes" "no")
that clearly it is a function application that returns the first parameter passed, as the lambda function does that.
The first case is similar, but in this case, it is the function itself which is passed as an argument. You have:
(<Function> (lambda (a b) a))
where this Function
accepts "something" that can be put to build an application expression and produce a result:
(lambda (proc) (proc "yes" "no"))
that is, accepts some function proc
, and "calls" it with two parameters. Note that the function you pass, (lambda (a b) a)
accepts two parameters and returns the first, so it produces "yes"
again.
Upvotes: 1