Question about lambda

Discussion of Common Lisp
Post Reply
August
Posts: 17
Joined: Fri Oct 03, 2008 1:41 pm
Location: Los Alamos, New Mexico USA
Contact:

Question about lambda

Post by August » Tue May 31, 2011 9:23 pm

On my Lisp journey, there is something about lambda that has been bugging me.

;; This works, as expected.
((lambda (n) (* n n)) 3)

;; This doesn't work, as expected;
(#'(lambda (n) (* n n)) 3)

;; This works, as expected.
(mapcar #'(lambda (n) (* n n)) '(1 2 3))

;; Why does this work also?
(mapcar (lambda (n) (* n n)) '(1 2 3))

I didn't expect this last case to work, assuming that you need to get the function value of the lambda to pass it as an argument.
---------
DarklingX, LLC
http://www.darklingx.com

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Question about lambda

Post by ramarren » Tue May 31, 2011 10:04 pm

In operator position (as considered by the evaluator) LAMBDA is not treated specially, as it is when it is in a form in function name position (as in the first and third of your examples), but rather as a perfectly standard macro which expands to (function (lambda ...)).

August
Posts: 17
Joined: Fri Oct 03, 2008 1:41 pm
Location: Los Alamos, New Mexico USA
Contact:

Re: Question about lambda

Post by August » Wed Jun 01, 2011 9:57 am

Thanks Ramarren. I guess I need to read through the hyperspec more thoroughly :-)

Style-wise, do people generally use the #' when using lambda as a function argument and when returning a lambda from a function?
---------
DarklingX, LLC
http://www.darklingx.com

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Question about lambda

Post by ramarren » Wed Jun 01, 2011 10:14 am

I do not believe there is consensus on this. I personally like to use #' to additionally indicate that an anonymous function is returned. Since the LAMBDA macro was added relatively late in the standardisation process this can be considered "old-style", but Practical Common Lisp uses it as well.

August
Posts: 17
Joined: Fri Oct 03, 2008 1:41 pm
Location: Los Alamos, New Mexico USA
Contact:

Re: Question about lambda

Post by August » Wed Jun 01, 2011 10:33 am

OK, thanks much for the help!
---------
DarklingX, LLC
http://www.darklingx.com

findinglisp
Posts: 447
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX
Contact:

Re: Question about lambda

Post by findinglisp » Sun Jun 05, 2011 5:40 pm

August wrote:Style-wise, do people generally use the #' when using lambda as a function argument and when returning a lambda from a function?
Generally, yes, people use #'(LAMBDA ...).
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/

Post Reply