This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Question about lambda

6 posts · 1556 views

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

Re: Question about lambda

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 ...)).

Re: Question about lambda

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

Re: Question about lambda

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.

Re: Question about lambda

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/