Recursion<!-- --> | LearnFP

Recursion

Skills: functions recursion

Recursion basics

Recursion is a technique in which a function calls itself.

There are two parts to recursion:

  1. A recursive call: calling the function again with a simpler input
  2. The base case: the final case where you stop calling the function

A simple example is a factorial problem. A factorial of n multiples all the numbers from 1 to n. For example, 5! is 5*4*3*2*1

How would we write a recursive function to solve this?

  1. Recursive call: we would want to multiply each number by the number below it, by the number below it, etc.
  2. Base case: once we reach 1, we stop

Code:

(define (factorial n)
(if (= n 1)
1
(* n (factorial (- n 1))))
)

Tail recursion

Tail recursion is a special type of recursion in which the value to return is tracked throughout the recursion.