Page 1 of 1

Why does this "let" work?

Posted: Mon Jul 25, 2011 3:36 pm
by amachina

Code: Select all

(defun add ()
...(let ((sum 0) next)
......(loop
.........(setq next (read))
.........(cond ((numberp next) (incf sum next))
............(eq '= next) (print sum) (return))
............(t (format t "~&~a ignored~%" next))))
.......(values)))
I thought the structure of a let is:

(let (bindings) forms)
where (bindings) is any number of two-element lists:

Code: Select all

(let ((a 0)
       (b 1))
  (form))
Above, (let ((sum 0) next) ...
does not seem to follow that rule

(BTW: How do I write lisp code here so that it is nicelfy formatted --- sorry - haven't had a chance to read the FAQ yet -- I will)

Re: Why does this "let" work?

Posted: Mon Jul 25, 2011 6:14 pm
by gugamilare
When no value is given to a variable being declared with let, it is assigned to NIL

Therefore, this:

Code: Select all

(let ((sum 0) next)
  ...)
this:

Code: Select all

(let ((sum 0) (next))
  ...)
and this:

Code: Select all

(let ((sum 0) (next nil))
  ...)
they all mean the same thing.

Note: to insert Lisp code in this forum, put it like this:

Code: Select all

[code]<Lisp code here>
[/code]

Re: Why does this "let" work?

Posted: Tue Jul 26, 2011 9:05 am
by amachina
Thanks. That allows me to move forward. As you said, all of these are equivalent:

Code: Select all

(let ((a 0) b)
 
(let ((a 0)
      (b nil))

(let ((a 0)
      (b))
I like the middle one because it "follows the rules".

Re: Why does this "let" work?

Posted: Tue Jul 26, 2011 10:35 am
by gugamilare
You are welcome :)

Re: Why does this "let" work?

Posted: Tue Jul 26, 2011 2:54 pm
by Paul
amachina wrote:Thanks. That allows me to move forward. As you said, all of these are equivalent:

Code: Select all

(let ((a 0) b)
 
(let ((a 0)
      (b nil))

(let ((a 0)
      (b))
I like the middle one because it "follows the rules".
They all "follow the rules"...