我最近开始阅读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