Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a value from a loop

Tags:

clojure

I'm new to Clojure and was hoping SO could help me out here:

;; 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)))

How do I, after collecting the input, get a list of all the input collected thus far?

like image 312
lowerkey Avatar asked Sep 01 '25 03:09

lowerkey


1 Answers

the return value of the loop will be the retrun value of the if statement

 (loop [ins inputs]
   (def input (read-val ">"))
   (if (not= input "0")
     (recur (conj ins input))
     return-value-goes-here))

and replace def with let for binding locals

(loop [ins inputs]
 (let [input (read-val ">")]
  (if (not= input "0")
    (recur (conj ins input))
    return-value-goes-here)))
like image 124
Arthur Ulfeldt Avatar answered Sep 04 '25 16:09

Arthur Ulfeldt