我已经写了一个小的递归函数,但是,如果我在let块中包含递归位,它会抛出一个不支持的绑定错误。
代码:
(defn total-weight [parcel cost]
"Calculate the total route cost"
(if (not(empty? parcel))
(let [ first-parcel (first parcel)
weight (:weight first-parcel)
parcel-two (rest parcel)
(total-weight parcel-two (+ cost weight))
cost])))
(total-weight task5 0)错误:
CompilerException java.lang.Exception: Unsupported binding form: (total-weight parcel-two (+ cost weight)), compiling:(/private/var/folders/2h/7b4v1ls11mjf_n5hb6mwngl40000gn/T/form-init2186446380943426996.clj:4:6) 有什么想法吗?
发布于 2017-03-22 01:16:05
虽然这是练习递归思维的一个很好的练习,但请注意,作为一个经验丰富的Clojure程序员,实现它的“最佳”方法是:
(apply + (map :weight task5))如此简单,甚至不需要为它定义一个函数。
发布于 2017-03-21 21:47:13
您的函数应该如下所示:
(defn total-weight [parcel cost] "Calculate the total route cost"
(if (not (empty? parcel))
(let [first-parcel (first parcel)
weight (:weight first-parcel)
parcel-two (rest parcel)]
(total-weight parcel-two (+ cost weight))
cost)))let in bind应该是这样的
(let [x1 x2] (print x1))您的]放错了位置。
https://stackoverflow.com/questions/42928799
复制相似问题