Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Recursion Clojure:你能在递归函数中找到它吗?_Recursion_Clojure - Fatal编程技术网

Recursion Clojure:你能在递归函数中找到它吗?

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

我希望final在调用(系统/出口0)时等于“false-true-false”。我相信这与康卡特有关。任何帮助都将不胜感激。

这里有两个问题:

  • 您应该使用
    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

 )