A fundamental syntax confusion

Discussion of Common Lisp

A fundamental syntax confusion

Postby 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!
wvxvw
 
Posts: 125
Joined: Sat Mar 26, 2011 6:23 am

Re: A fundamental syntax confusion

Postby 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))
gugamilare
 
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

Re: A fundamental syntax confusion

Postby wvxvw » Fri Feb 10, 2012 6:53 am

Aha, thank you, that makes sense now!
wvxvw
 
Posts: 125
Joined: Sat Mar 26, 2011 6:23 am


Return to Common Lisp

Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 2 guests

cron