Question about lambda
Question about lambda
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.
;; 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.
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?

Style-wise, do people generally use the #' when using lambda as a function argument and when returning a lambda from a function?
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
OK, thanks much for the help!
-
- Posts: 447
- Joined: Sat Jun 28, 2008 7:49 am
- Location: Austin, TX
- Contact:
Re: Question about lambda
Generally, yes, people use #'(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?
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/