clojure中的递归 [英] recursion in clojure
本文介绍了clojure中的递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图让这个示例程序工作
I am trying to make this sample program work
(defn foo
([x] (foo x []))
([x current]
(when (> x 0)
(recur (dec x) (conj current x)))))
当我调用这个函数(foo 5)时,我应该得到[1 2 3 4 5],但是它只返回nil。我做错了什么?
When I call this function (foo 5), I should get [1 2 3 4 5], however it only returns nil. What am I doing wrong?
感谢,
Murtaza
Thanks, Murtaza
推荐答案
我已经更正了您的原始程序使用(if(= x 0)
而不是(when(> x 0)
,并返回 [1 2 3 4 5]
。
I have corrected your original program to use (if (= x 0)
instead of (when (> x 0)
, and this returns [1 2 3 4 5]
.
(defn foo
([x] (foo x []))
([x current]
(if (= x 0)
(apply vector (sort < current))
(recur (dec x) (conj current x)))))
这篇关于clojure中的递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文