I'm a programming beginner, switching from Python to Clojure now. I am working on a prime number code, but I don't get the mistake. I want to practice recursion also
(defn true-division [n i]
(= (/ n i) (quot n i)))
;proves if division is possible
(defn is-prime-number2 [n]
(def i (atom (- n 1)))
(while (> @i 1)
(do
(conj list (true-division n @i))
(swap! @i dec))))
;should fill an empty list with true and false statements
;in the final function I want to check if there is a true in the list
(println (is-prime-number2 12))
2nd Code as a recursive try:
(defn true-division [n i]
(= (/ n i) (quot n i))
)
(println (true-division 4 5))
(defn is-prime-number [n]
(let [list []]
(loop [i (- n 1)]
(while(> i 0)
(do
(conj list (true-division n i))
(recur (dec i)))))))
(println (is-prime-number 12))
My preferred way would be to stay in a loop/iteration - if there comes a true, then interrupt the loop.
I don't know how to code this with the basics I learned so far. If I'm creating loops, I always struggle with breaking them when the condition is reached.
In this example I would create a final function with an if to prove if there is a true in the list and print out the result.
I heard about the lazy function but I want to practice the syntax and functions, so I would appreciate your help on the way I planned :)