15. .
大牛们怎么说 Lisp 的
.
Eric Raymond
.
“Lisp is worth learning for the profound enlightenment
experience you will have when you finally get it; that experience
will make you a better programmer for the rest of your days, even
if you never actually use Lisp itself a lot.”
.
15 /
Jerry Peng JVM 上的实用 Lisp 方言:Clojure 90
15/90
.
16. .
大牛们怎么说 Lisp 的
.
Paul Graham
.
“Within a couple weeks of learning Lisp I found programming in
any other language unbearably constraining.”
.
.
Alan Kay
.
“The greatest single programming language ever designed.”
.
16 /
Jerry Peng JVM 上的实用 Lisp 方言:Clojure 90
16/90
.
26. .
Lisp 求值规则(2)
• atom 求值规则
◦ symbol 求值得到 symbol 上绑定的值
◦ 其他 atom 求值得到本身
• form 求值规则
◦ 根据 form 的第一个 symbol 决定语义
26 /
Jerry Peng JVM 上的实用 Lisp 方言:Clojure 90
26/90
.
27. .
form 的语义由第一个 symbol 决定
• symbol 对应一个函数:函数调用
◦ 要先将函数所有的参数求值出来才能调用函数
• symbol 对应一个 special form
◦ 根据 special form 来决定语义,且子 form 代表的含义是固定
的
• symbol 对应一个宏
◦ 调用宏函数来展开宏
◦ 这一步一般发生在编译阶段,到运行时所有的宏都应该被完
全展开了
27 /
Jerry Peng JVM 上的实用 Lisp 方言:Clojure 90
27/90
.
44. .
局部变量的使用——let
(let [a 3
b 4
c (* a b)] ; 引用已定义的 symbol a 和 b
(println a b c))
; Body 中可以引用 let 中绑定的所有 symbol
• 定义局部变量
• 将值绑定到指定的 symbol 上,在 let 的 body 部分可以引
用这些 symbol
◦ 可一次绑定多个 symbol
◦ 后面的 binding form 中可以引用前面已经定义过的 symbol
• 绑定是只读的,一旦绑定无法变更
◦ Clojure 强调“不可变性”的体现
44 /
Jerry Peng JVM 上的实用 Lisp 方言:Clojure 90
44/90
.
45. .
let 可以任意嵌套
(let [a 3
b 4]
(let [a 5
b (+ b 1)
c (* a b)]
(println a b c)))
• Clojure 采用的是词法作用域,symbol 引用的是最近的
scope 里的值
• let 的 [..] 部分被成为 binding form
◦ 其他的 binding form 还有函数参数
◦ binding 语句
• 嵌套的 binding form 都采用上述规则
45 /
Jerry Peng JVM 上的实用 Lisp 方言:Clojure 90
45/90
.