Updated April 7, 2023
Introduction to Clojure loop
Clojure has a special kind of loop, unlike other programming languages, which sets a recursion point by making use of recur, meaning recur is always associated with loop. A loop is said to be completed when the number of arguments passed to the recur is the same as the number of bindings for the loop, ensuring that the recur is in sync with the loop. There is a base case associated with every recursive function based on which the looping stops, and by making use of the loop, we are implying the compiler to try its best to optimize the code for recursion.
Syntax to declare loop in Clojure:
loop [binding]
(condition
(statement)
(recur (binding)))
Binding is the number of bindings for the loop, which is the same as the recur function arguments.
Flowchart
Flowchart for the working of the loop in Clojure is as follows:
Working of the loop in Clojure
- The loop begins with the binding variable followed by the evaluation of the condition.
- If the condition is true, the control goes to statements specified inside the condition and executes them.
- If the condition is false, the loop stops.
- After the execution of statements inside the condition, the control again goes to the binding variable passed as a parameter to the recur function, completing a loop and continues the execution of the loop again.
- The same procedure continues until the condition is false and the loop stops.
Examples of Clojure loop
Given below are the examples of Clojure loop:
Example #1
Clojure program to demonstrate a loop using recur function to print the numbers starting from 20 decremented by 1 in every step and display it as the output on the screen.
Code:
;; defining a function called decrement to decrease the values by 1 starting from 20
(defn decrement []
(loop [y 20]
(when (> y 0)
(println y)
(recur (- y 1)))))
(decrement)
Output:
In the above program, we are defining a function called decrement to decrease the given value by one after each iteration. The binding value to the loop is 20, which is decremented by 1 in the recur function and continues till the condition that the value of the variable y is greater than 0.
Example #2
Clojure program to demonstrate a loop using recur function to print the numbers starting from 0 incremented by 1 in every step and display it as the output on the screen.
Code:
;; defining a function called increment to increase the values by 1 starting from 0 to 20
(defn increment []
(loop [y 0]
(when (< y 21)
(println y)
(recur (+ y 1)))))
(increment)
Output:
In the above program, we are defining a function called increment to increase the given value by one after each iteration. The binding value to the loop is 0, which is incremented by 1 in the recur function and continues till the condition that the value of the variable y is lesser than 21.
Example #3
Clojure program to demonstrate a loop using recur function to print the numbers starting from 2 multiplied by 2 in every step whose result continues till the value of the variable is less than 21 and displays it as the output on the screen.
Code:
;; defining a function called multiply to multiply the given value by 2 starting from 2 till the result value becomes less than 21
(defn multiply []
(loop [y 2]
(when (< y 21)
(println y)
(recur (* y 2)))))
(multiply)
Output:
In the above program, we are defining a function called multiply to multiply the given value by two after each iteration. The binding value to the loop is 2, which is multiplied by 2 in the recur function and continues until the condition that the variable y becomes lesser than 21.
Example #4
Clojure program to demonstrate a loop using recur function to print the numbers starting from 20 divided by 2 in every step whose result continues till the value of the variable is less than 1 and displays it as the output on the screen.
Code:
;; defining a function called divide to divide the given value by 2 starting from 20 till the result value becomes less than 1
(defn divide []
(loop [y 20]
(when (> y 1)
(println y)
(recur (/ y 2)))))
(divide)
Output:
In the above program, we are defining a function called divide to divide the given value by two after each iteration. The binding value to the loop is 20, which is divided by 2 in the recur function and continues till the condition that the value of the variable y becomes lesser than 1.
Example #5
Clojure program to demonstrate a loop using recur function to print the numbers starting from 20 decremented by 5 in every step and display it as the output on the screen.
Code:
;; defining a function called decrement to decrease the values by 5 starting from 20
(defn decrement []
(loop [y 20]
(when (> y 0)
(println y)
(recur (- y 5)))))
(decrement)
Output:
In the above program, we are defining a function called decrement to decrease the given value by five after each iteration. The binding value to the loop is 20, which is decremented by 5 in the recur function and continues till the condition that the value of the variable y is greater than 0.
Recommended Articles
We hope that this EDUCBA information on “Clojure loop” was beneficial to you. You can view EDUCBA’s recommended articles for more information.