Want this course?
map with tail recursion
In the sixth lesson in _Recursion 101, _we rewrite map to be tail recursive. Any non-tail recursive function can be rewritten in a tail-recursive way, and this is an example. Using tail recursion eliminates the danger of overflowing the stack. However, it can create an infinite loop if the input sequence is infinite.
(defn map*-helper [f ls acc] (if (empty? ls) acc (recur f (rest ls) (conj acc (f (first ls))))))
(defn map* [f ls] (map*-helper f ls ))