最近我一直在学习Clojure,看到人们在不同的地方使用[], '[], ()和'()。在某些情况下,[]和'[]是可以互换的。
所以我想知道这两个符号之间的区别是什么?
最近我一直在学习Clojure,看到人们在不同的地方使用[], '[], ()和'()。在某些情况下,[]和'[]是可以互换的。
所以我想知道这两个符号之间的区别是什么?
请阅读有关宏
和引用
的主题。
简而言之,'
是一个阅读器宏,它扩展为(quote ..)
,因此'[]
等于(quote [])
它的作用是抑制评估(即此处a
未定义)
user=> (quote [a])
[a]
user=> '[a]
[a]
; the following will fails since "a" is not bound
; there is no way to construct an array where the
; first element is the value of a
user=> [a]
CompilerException java.lang.RuntimeException:
Unable to resolve symbol: a in this context,
compiling:(NO_SOURCE_PATH:0:0)