【发布时间】:2014-12-28 05:20:12
【问题描述】:
学习 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)
))))
以及下面的类 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 the value (* acc cnt)
clojure 的“循环”和“重复”形式是专门为编写简单的命令式循环而设计的吗? (假设伪代码的“for”创建了它自己的作用域,所以 cnt 和 acc 只存在于循环内)
【问题讨论】:
标签: clojure