Newbie Lisp question, sorry for the ignorance.
What is the equivalent of Common Lisp's do in Clojure?
Clojure avoids these kind of sequential binding forms, but the same functionality can be expressed with while or loop - the first example from the CLHS in each style:
;; common lisp version
(do ((temp-one 1 (1+ temp-one))
(temp-two 0 (1- temp-two)))
((> (- temp-one temp-two) 5) temp-one)) => 4
;; clojure, while
(let [temp-one (atom 1)
temp-two (atom 0)]
(while (> (- @temp-one @temp-two) 5)
(swap! temp-one inc)
(swap! temp-two dec))
@temp-one)
;; clojure, loop
(loop [temp-one 1 temp-two 0]
(if (> (- temp-one temp-two) 5)
temp-one
(recur (inc temp-one) (dec temp-two))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With