Recursion Clojure:你能在递归函数中找到它吗?
我希望final在调用(系统/出口0)时等于“false-true-false”。我相信这与康卡特有关。任何帮助都将不胜感激。这里有两个问题:Recursion Clojure:你能在递归函数中找到它吗?,recursion,clojure,Recursion,Clojure,我希望final在调用(系统/出口0)时等于“false-true-false”。我相信这与康卡特有关。任何帮助都将不胜感激。这里有两个问题: 您应该使用cons将元素放入列表 这个递归函数需要一个停止条件,在本例中使用if 您最好在函数体中使用recur而不是相同的函数名 (defn recurse\u fun [最后输入] (if(空?输入);递归调用的'STOP'条件 final;要返回的最终值 (续) (='false(第一次输入))(recur(休息输入)(cons'true fi
- 您应该使用
将元素放入列表cons
- 这个递归函数需要一个停止条件,在本例中使用
if
- 您最好在函数体中使用
而不是相同的函数名recur
(defn recurse\u fun
[最后输入]
(if(空?输入);递归调用的'STOP'条件
final;要返回的最终值
(续)
(='false(第一次输入))(recur(休息输入)(cons'true final));请使用cons代替concat
;通常在Clojure中使用“重现”`
(='true(第一次输入))(recur(rest输入)(cons'false final));我猜这是你的拼写错误?
)
))
为什么在第一站它是cond
?它是否应该使用if
?(cond x y)
宏扩展为(if x y)
谢谢。为什么使用cons而不是concat?为什么我的cond stop条件不起作用?系统。退出(0)
将退出当前进程,而不是退出当前函数,并返回最终值cons
将元素放入列表/向量,但concat
正在连接集合(表示集合作为输入)
(defn recurse_fun ;assume user types in (recurse_fun '(true false true) "")
[input final]
(cond
(empty? input) (System/exit 0) ;at this point final should equal (false true false)
)
(cond
(= 'false (first input)) (recurse_fun (rest input) (concat 'true final))
;^ recurse with new parameters of the rest of input and true concated with previous final
(= 'true (first input)) (recurse_fun (rest input) (concat 'false final))
;^ recurse with new parameters of the rest of input and false concated with previous final
)