我最近開始閱讀Paul Grahams的On Lisp,並學習了clojure,所以這裏可能有一些非常明顯的錯誤,但我看不到它:(顯然,它是一個項目問題)這個Clojure程序有什麼問題?
(ns net.projecteuler.problem31)
(def paths (ref #{}))
; apply fun to all elements of coll for which pred-fun returns true
(defn apply-if [pred-fun fun coll]
(apply fun (filter pred-fun coll)))
(defn make-combination-counter [coin-values]
(fn recurse
([sum] (recurse sum 0 '()))
([max-sum current-sum coin-path]
(if (= max-sum current-sum)
; if we've recursed to the bottom, add current path to paths
(dosync (ref-set paths (conj @paths (sort coin-path))))
; else go on recursing
(apply-if (fn [x] (<= (+ current-sum x) max-sum))
(fn [x] (recurse max-sum (+ x current-sum) (cons x coin-path)))
coin-values)))))
(def count-currency-combinations (make-combination-counter '(1 2 5 10 20 50 100 200)))
(count-currency-combinations 200)
當我運行在REPL的最後一行,我得到的錯誤:
<#CompilerException java.lang.IllegalArgumentException: Wrong number of args passed to: problem31$eval--25$make-combination-counter--27$recurse--29$fn (NO_SOURCE_FILE:0)>
除了錯誤所在的問題,更有趣的問題是:如何將一個調試呢?錯誤消息不是很有幫助,我還沒有找到單步完成clojure代碼的好方法,而且每次遇到問題時我都無法真正地詢問堆棧溢出問題。
優秀的問題。錯誤消息可能相當有敵意! – 2009-11-10 21:57:04