2015-08-20 36 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