Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure: equivalent of "do" from Common Lisp

Newbie Lisp question, sorry for the ignorance.

What is the equivalent of Common Lisp's do in Clojure?

like image 316
albusshin Avatar asked Nov 28 '25 18:11

albusshin


1 Answers

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))))
like image 155
noisesmith Avatar answered Dec 01 '25 00:12

noisesmith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!