This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Why does this "let" work?

5 posts · 1739 views

(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:
(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?

When no value is given to a variable being declared with let, it is assigned to NIL

Therefore, this:
(let ((sum 0) next)
  ...)
this:
(let ((sum 0) (next))
  ...)
and this:
(let ((sum 0) (next nil))
  ...)
they all mean the same thing.

Note: to insert Lisp code in this forum, put it like this:
[code]<Lisp code here>
[/code]

Re: Why does this "let" work?

Thanks. That allows me to move forward. As you said, all of these are equivalent:
(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?

You are welcome :)

Re: Why does this "let" work?

amachina wrote:Thanks. That allows me to move forward. As you said, all of these are equivalent:
(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"...