A fundamental syntax confusion

Discussion of Common Lisp
Post Reply
wvxvw
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

A fundamental syntax confusion

Post by wvxvw » Fri Feb 10, 2012 3:31 am

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!

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: A fundamental syntax confusion

Post by gugamilare » Fri Feb 10, 2012 5:23 am

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

wvxvw
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

Re: A fundamental syntax confusion

Post by wvxvw » Fri Feb 10, 2012 6:53 am

Aha, thank you, that makes sense now!

Post Reply