Page 1 of 1 [ 5 posts ] 

Fuzzy
Veteran
Veteran

User avatar

Joined: 30 Mar 2006
Age: 53
Gender: Male
Posts: 5,223
Location: Alberta Canada

13 May 2009, 2:52 pm

I'm reading up onPLT Scheme in order to come to understand it. One thing that is just escaping me is the use of the lambda function. Can anyone explain this?


_________________
davidred wrote...
I installed Ubuntu once and it completely destroyed my paying relationship with Microsoft.


ruveyn
Veteran
Veteran

User avatar

Joined: 21 Sep 2008
Age: 89
Gender: Male
Posts: 31,502
Location: New Jersey

13 May 2009, 3:09 pm

Fuzzy wrote:
I'm reading up onPLT Scheme in order to come to understand it. One thing that is just escaping me is the use of the lambda function. Can anyone explain this?


It is from Church's Lambda Calculus and is used to denote a function as opposed to a value computed by a function.

See
http://en.wikipedia.org/wiki/Lambda_calculus

ruveyn



TheKingsRaven
Deinonychus
Deinonychus

User avatar

Joined: 16 Feb 2009
Age: 37
Gender: Male
Posts: 306
Location: UK

13 May 2009, 4:35 pm

In lisp functions are just data, like a number or a string. Lambda is a function that returns a function.

Here is a psudo code example because its late at night and I can't be bothered to go back and relearn correct syntax:

(lambda (+ 5 %)) will output to a function, the new function takes one argument and adds 5 to it, although the % might be unique to the Clojure dialect of lisp.



Fuzzy
Veteran
Veteran

User avatar

Joined: 30 Mar 2006
Age: 53
Gender: Male
Posts: 5,223
Location: Alberta Canada

13 May 2009, 6:40 pm

TheKingsRaven wrote:
In lisp functions are just data, like a number or a string. Lambda is a function that returns a function.

Here is a psudo code example because its late at night and I can't be bothered to go back and relearn correct syntax:

(lambda (+ 5 %)) will output to a function, the new function takes one argument and adds 5 to it, although the % might be unique to the Clojure dialect of lisp.



Ah! Its almost like a macro. I get it now. The % is a place holder. In PLT scheme I can specify a variable.

(lambda (something) (+ 5 something))

and when I make use of it, it will insert code into my program, rather than returning a number. It then also resolves as a normal function would.

In C I might say

Code:
int addthis(something) {
 return something +=5;
}


But in the case of lisp, I dont need to give it a name. Its useful for a one shot function perhaps.

Right? Right?


_________________
davidred wrote...
I installed Ubuntu once and it completely destroyed my paying relationship with Microsoft.


TheKingsRaven
Deinonychus
Deinonychus

User avatar

Joined: 16 Feb 2009
Age: 37
Gender: Male
Posts: 306
Location: UK

14 May 2009, 4:29 am

Fuzzy wrote:
Ah! Its almost like a macro. I get it now. The % is a place holder. In PLT scheme I can specify a variable.

(lambda (something) (+ 5 something))
I think so, I'm not sure the exact syntax of PLT scheme, % comes from clojure where its just syntactic sugar for the above.

Fuzzy wrote:
But in the case of lisp, I dont need to give it a name. Its useful for a one shot function perhaps.

Yes, you get a lot of one shot functions in lisp if your useing it right. Take this java code:

for (int i=0; i < array.length; i++){
array[i] = array[i] + 5;
}

you've probably written something like this 100 times, only the second line and the name of the array changes, so the lisp guys have distilled it down to its purest form: one function, two arguments: the name of the list and what you want to do to each cell:

(map plusfive list)

of course that means plusfive has to be a prewritten function somewhere, so much easier to use an annonimous function instead

(map (lambda (+ 5 %)) list)

And of course there's probably nice syntactic sugar for that, I don't know PLT scheme but in clojure it looks like this

(map #(+ 5 %) list)