2015-08-20 37 views
0

这个阶乘函数我有这样的功能:这有什么错用Clojure

(defn ! [x] 
    (let [n x product 1] 
    (if (zero? n) 
     product 
     (recur (- n 1) (* product n))))) 

,我得到了错误:java.lang.IllegalArgumentException: Mismatched argument count to recur, expected: 1 args, got: 2 (NO_SOURCE_FILE:33)

而是从其他SO问题,做工精细这种因子。为什么?

(defn fact [x] 
    (loop [n x f 1] 
     (if (= n 1) 
      f 
      (recur (dec n) (* f n))))) 
+1

替换为''let'循环“,那么你的”复发“会发现它。 – Thumbnail

回答

2

你不能recurlet

当你在这里recur,你实际上重复的函数定义有一个参数,因此java.lang.IllegalArgumentException: Mismatched argument count to recur, expected: 1 args, got: 2 (NO_SOURCE_FILE:33)

在第二个例子中,他使用的是loop,当你想在recur的其他参数上使用时,你应该使用这个函数。

+0

没有注意到有循环而不是让第二个例子。非常感谢。 – jcubic

2

在你的榜样recur循环,以!这是预期1个PARAM但得到2,

在第二个例子recur循环来loop这是期待2个PARAMS,并得到2个PARAMS