学习Clojure并尝试理解实现:
有什么区别:
(def factorial
(fn [n]
(loop [cnt n acc 1]
(if (zero? cnt)
acc
(recur (dec cnt) (* acc cnt))
; in loop cnt will take the value (dec cnt)
; and acc will take the value (* acc cnt)
))))
Run Code Online (Sandbox Code Playgroud)
和以下类似C的伪代码
function factorial (n)
for( cnt = n, acc = 1) {
if (cnt==0) return acc;
cnt = cnt-1;
acc = acc*cnt;
}
// in loop cnt will take the value (dec cnt)
// and acc will take …
Run Code Online (Sandbox Code Playgroud) 我正在分析LISP,我不是专家,但有些事情困扰着我:
一些原语list
接受多个参数.例如:
(list 1 2 3)
=> (1 2 3)
Run Code Online (Sandbox Code Playgroud)
另一方面,quote
似乎只接受一个参数.例如:
(quote (1 2 3))
=> (1 2 3)
(quote x)
=> 'x
(quote 1 2 3)
=> 1 ???
Run Code Online (Sandbox Code Playgroud)
有没有理由说(quote 1 2 3)
引用多个参数,只是忽略其他参数?
如果(quote 1 2 3)
评估会发生什么(1 2 3)
,即提供多个参数的特殊情况?
我知道这个特例是多余的,但我对LISP黑客的问题是:
添加这样的特殊情况quote
会破坏一切吗?它会破坏REPL吗?会破坏宏吗?