Skip to end of metadata
Go to start of metadata
Unknown macro: {style}


Unknown macro: {font-family}

Loops and recursion are equivalent – a language with only one is just as powerful (meaning it can solve as many problems, no comment about speed) as one with both. The functional paradigm encourages using recursion, but there are times when iteration is simpler or faster.

Recursion vs For Loop

In the Julia set example, we use recursion to build a 3-dimensional list to represent an image. This function is provided on the left. On the right is an alternative. It creates a 2-dimensional array and iteratively assigns a 3-list to each cell.

Unknown macro: {table}
Unknown macro: {tr}
Unknown macro: {td}
Unknown macro: {td}

Though these two functions look very different, they use approximately the same amount of memory. The CMUCL implementation of Lisp prints out the garbage collection events as they occur, and both functions require about 50 collections.

This is not to say that all forms of iteration are equal. The CMUCL manual points out that apply and mapcar, which are functions to iteratively modify the contents of an array or list, are slower than some iterative alternatives.

  • No labels