~/ learn/ comp-456/ cards/ Lisp idioms: s-expressions, car/cdr/cons, and recursion
1 of 5

Type the Lisp factorial definition

Type the Lisp factorial definition

Answer

(defun factorial (n) (if (= n 0) 1 (* n (factorial (- n 1)))))

defun names the function; if picks the branch. Base case: (= n 0) returns 1. Recursive step: (* n (factorial (- n 1))). Prefix form means the operator leads — (* n ...) is n times .... Same structure as the Prolog fact/2 and the Python factorial.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/c8dd5aa7-e75b-4340-94ab-fa9d3e6eb4e1/flashcard utf-8 LF