我是Clojure的新手,希望SO能帮到我这里:
;; a loop to collect data
(defn read-multiple []
(def inputs (list))
(loop [ins inputs]
(def input (read-val ">"))
(if (not= input "0")
(recur (conj ins input)))
(i-would-like-to-return-something-when-the-loop-terminates)))
收集输入后,如何获取到目前为止收集的所有输入的列表?
答案 0 :(得分:3)
循环的返回值将是if语句的返回值
(loop [ins inputs]
(def input (read-val ">"))
(if (not= input "0")
(recur (conj ins input))
return-value-goes-here))
并将def
替换为let
以绑定本地人
(loop [ins inputs]
(let [input (read-val ">")]
(if (not= input "0")
(recur (conj ins input))
return-value-goes-here)))