Tail recursion

Fixme work in progress

If we generate a very large collection we run the risk of blowing our heap space. For example we could use range to generate a very large collection, say a vector containing 10 billion values

Dont try this example below

  1. (vec (range 0 9999999999))
  2. ;; this will crash after a short while as it will use up all your heap space

Using tail call optomisation (tail recursion) allows us to reuse a memory location when we call a function recursively. This tail recursion is not part of the underlying Java Virtual Machine (JVM), so instead Clojure has a specific function called recur

The recur function allows the processing of a very large data set without blowing the heap space because the memory space will be reused.

The recur function must be the last expression in order to work.

  1. (defn sum
  2. ([vals] (sum vals 0))
  3. ([vals accumulating-total]
  4. (if (empty? vals)
  5. accumulating-total
  6. (recur (rest vals) (+ (first vals) accumulating-total)))))
  7. (sum (vec (range 0 9999999)))