从循环返回值

时间:2012-06-28 18:13:45

标签: clojure

我是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)))

收集输入后,如何获取到目前为止收集的所有输入的列表?

1 个答案:

答案 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)))