Prev Up Next

A procedure body can contain calls to other procedures, not least itself:

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

This recursive procedure calculates the factorial of a number. If the number is 0, the answer is 1. For any other number n, the procedure uses itself to calculate the factorial of n - 1, multiplies that subresult by n, and returns the product.

Mutually recursive procedures are also possible. The following predicates for evenness and oddness use each other:

(define is-even?
  (lambda (n)
    (if (= n 0) #t
        (is-odd? (- n 1)))))

(define is-odd?
  (lambda (n)
    (if (= n 0) #f
        (is-even? (- n 1)))))

Prev Up Next

Log in or register to write something here or to contact authors.