Read as text

A Lisp Example: Recursive Factorial Function

One classic example is calculating a factorial. In Lisp, the factorial function can be defined recursively as follows:

(defun factorial (n) (if (<= n 1)

  1
  (* n (factorial (- n 1)))))

In this definition, if n is 1 or less (the base case), the function returns 1. Otherwise, it multiplies n by the factorial of (n-1), further breaking the problem into a simpler one until reaching the base case.

Actions

Node Information

/What is Lisp?node=aa3

Ask Questions & Add Comments