Page 1 of 1

A fundamental syntax confusion

Posted: Fri Feb 10, 2012 3:31 am
by wvxvw

Code: Select all

(defparameter *test-symbol*
  ((lambda (x) (format t "x: ~a~&" x) x)
   (lambda (y) (format t "y: ~a~&" y) y)))
I've come across a similar construct in someone else's code. It works, but I'm struggling to understand what does it do, and no luck so far :) Can someone, please, explain what is being saved into *test-symbol*? I could only figure so far that it is callable and what gets executed is the (lambda (y) ...), but what (lambda (x) ...) does and why it gets called when the entire construct is being defined is beyond me. Lastly, no matter what first lambda returns, the construct seem to generate no problems, however, if I replace the first lambda by it's return value, of course I get an error.

Thanks!

Re: A fundamental syntax confusion

Posted: Fri Feb 10, 2012 5:23 am
by gugamilare
There is a special evaluation for lambda (see the Notes in its specification). For instance:

Code: Select all

((lambda (x) (+ x 3))
 5)
is the same as

Code: Select all

(funcall #'(lambda (x) (+ x 3))
 5)
or

Code: Select all

(let ((x 5))
  (+ x 3))
Your code is equivalent to

Code: Select all

(defparameter *test-symbol*
  (let ((x (lambda (y) (format t "y: ~a~&" y) y)))
    (format t "x: ~a~&" x)
    x))

Re: A fundamental syntax confusion

Posted: Fri Feb 10, 2012 6:53 am
by wvxvw
Aha, thank you, that makes sense now!