Recursion 未在let块中评估函数
我已经编写了一个小的递归函数,但是,如果我在let块中包含递归位,它会抛出一个未移植的绑定错误 守则:Recursion 未在let块中评估函数,recursion,clojure,Recursion,Clojure,我已经编写了一个小的递归函数,但是,如果我在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 p
(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)
有什么想法吗?您的函数应该是这样的:
(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 [x1 x2] (print x1))
您的
]
位于错误的位置。您的函数应该是这样的:
(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 [x1 x2] (print x1))
您的
]
放错了地方。虽然这是练习递归思维的一个很好的练习,但请注意,作为一名经验丰富的Clojure程序员,实现这一点的“最佳”方法是:
(apply + (map :weight task5))
非常简单,甚至不需要为其定义函数。虽然这是练习递归思维的一个很好的练习,但请注意,作为一名经验丰富的Clojure程序员,实现这一点的“最佳”方法是:
(apply + (map :weight task5))
它非常简单,甚至不需要为它定义函数。该函数的计算结果为0,但应该是3。这不是更新成本吗?函数的计算结果是0,但应该是3。这不是更新成本吗?