The list is lazily evaluated by recur loop code.
You can try it yourself. Let make permutations print something every time it returns a value by adding a call to println .
(defn permutations [s] (lazy-seq (if (seq (rest s)) (apply concat (for [xs] (map
When using loop also print the values ββwhen we get to (prn (first perms) .
(loop [perms (permutations [1 2 3])] (if (empty? perms) (prn "finised") (do (prn (first perms)) (recur (rest perms)))))
Here is what it prints:
returning a value (1 2 3) returning a value (1 3 2) returning a value (2 1 3) returning a value (2 3 1) returning a value (3 1 2) returning a value (3 2 1) "finished"
As you can see, the "return value" and the string of values ββalternate. Evaluating a lazy seq can be called using doall . If you are fixated on (doall (permutations [1 2 3])) , first it prints all the lines of "return value" and only the value.
source share