糯米

TI DaVinci, gstreamer, ffmpeg
随笔 - 167, 文章 - 0, 评论 - 47, 引用 - 0
数据加载中……

lisp let,let*

let and let* create new variable bindings and execute a series of forms that use these bindings. 
let performs the bindings in parallel and let* does them sequentially.

The form

(let ((var1 init-form-1)
(var2 init-form-2)
...
(varm init-form-m))
declaration1
declaration2
...
declarationp
form1
form2
...
formn)

first evaluates the expressions init-form-1, init-form-2, and so on, in that order, saving the resulting values.
Then all of the variables varj are bound to the corresponding values;
each binding is lexical unless there is a special declaration to the contrary.
The expressions formk are then evaluated in order; the values of all but the last are discarded
(that is, the body of a let is an implicit progn).
let* is similar to let, but the bindings of variables are performed sequentially rather than in parallel.
The expression for the init-form of a var can refer to vars previously bound in the let*.

The form

(let* ((var1 init-form-1)
(var2 init-form-2)
...
(varm init-form-m))
declaration1
declaration2
...
declarationp
form1
form2
...
formn)
first evaluates the expression init-form-1, then binds the variable var1 to that value;
then it evaluates init-form-2 and binds var2, and so on.
The expressions formj are then evaluated in order;
the values of all but the last are discarded (that is, the body of let* is an implicit progn).

For both let and let*, if there is not an init-form associated with a var, var is initialized to nil.

The special form let has the property that the scope of the name binding does not include any initial value form.
For let*, a variable's scope also includes the remaining initial value forms for subsequent variable bindings.


Examples:

(setq a 'top) => TOP
(defun dummy-function () a) => DUMMY-FUNCTION
(let ((a 'inside) (b a))
(format nil "~S ~S ~S" a b (dummy-function))) => "INSIDE TOP TOP"
(let* ((a 'inside) (b a))
(format nil "~S ~S ~S" a b (dummy-function))) => "INSIDE INSIDE TOP"
(let ((a 'inside) (b a))
(declare (special a))
(format nil "~S ~S ~S" a b (dummy-function))) => "INSIDE TOP INSIDE"

posted on 2011-08-22 11:50 糯米 阅读(786) 评论(1)  编辑 收藏 引用 所属分类: Lisp

评论

# re: lisp let,let*  回复  更多评论   

写的很详细,有点理解了。原来 let* 会把上一个表达式的计算结果带到下一个计算结果上面去:)
2015-10-24 19:58 | creamidea

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理