11.07.2015 Views

common lisp hints

common lisp hints

common lisp hints

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1 COMMON LISP HINTS 81.9 BindingBinding is lexically scoped assignment. It happens to the variables in a function's parameter list wheneverthe function is called: the formal parameters are bound to the actual parameters for the duration of thefunction call. You can bind variables anywhere in a program with the let special form, which looks likethis:(let ((var1 val1)(var2 val2)...)body)let binds var1 to val1, var2 to val2, and so forth then it executes the statements in its body. Thebody of a let follows exactly the same rules that a function body does. Some examples:> (let ((a 3)) (+ a 1))4> (let ((a 2)(b 3)(c 0))(setq c (+ a b))c)5> (setq c 4)4> (let ((c 5)) c)5> c4Instead of (let ((a nil) (b nil)) ...), you can write (let (a b) ...).The val1, val2, etc. inside a let cannot reference the variables var1, var2, etc. that the let is binding.For example,> (let ((x 1)(y (+ x 1)))y)Error: Attempt to take the value of the unbound symbol XIf the symbol x already has a global value, stranger happenings will result:> (setq x 7)7> (let ((x 1)(y (+ x 1)))y)8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!