注意:不重复为什么 Clojure recur 认为它应该只有一个参数?. 我没有使用循环。
(def t
#(let [[low high] (sort %&)] {:low low :high h}))
(t 3 2)
=> {:low 2, :high 3}
鉴于这按预期工作。这怎么没有:
(def t
#(let [[low high] (sort %&)]
(if (= 0 low)
nil
(do
(println {:low low :high high})
(recur low (dec high))))))
(t 3 2)
=> java.lang.IllegalArgumentException: Mismatched argument count to recur, expected: 1 args, got: 2
鉴于它说它需要 1 个参数,我可以猜测我可以通过将参数转换为集合来使其工作:
(def t
#(let [[low high] (sort %&)]
(if (= 0 low)
nil
(do
(println {:low low :high high})
(recur [low (dec high)])))))
(t 3 2)
=> {:low 2, :high 3}
{:low 2, :high 2}
{:low 1, :high 2}
{:low 1, :high 1}
nil
... 但为什么?