Tutorial by Examples

(defun fn (x) (cond (test-condition1 the-value1) (test-condition2 the-value2) ... ... ... (t (fn reduced-argument-x)))) CL-USER 2788 > (defun my-fib (n) (cond ((= n 1) 1) ((= n...
(defun fn (x) (cond (test-condition the-value) (t (fn reduced-argument-x))))
;;Find the nth Fibonacci number for any n > 0. ;; Precondition: n > 0, n is an integer. Behavior undefined otherwise. (defun fibonacci (n) (cond ( ;; Base case. ;; The first two Fibonacci numbers (indices 1 and 2) are 1 by defin...
;;Recursively print the elements of a list (defun print-list (elements) (cond ((null elements) '()) ;; Base case: There are no elements that have yet to be printed. Don't do anything and return a null list. (t ;; Recursive case ;; Print the next elem...
One easy algorithm to implement as a recursive function is factorial. ;;Compute the factorial for any n >= 0. Precondition: n >= 0, n is an integer. (defun factorial (n) (cond ((= n 0) 1) ;; Special case, 0! = 1 ((= n 1) 1) ;; Base case, 1! = 1 (t ...

Page 1 of 1