Skip to content

Instantly share code, notes, and snippets.

@topher6345
Last active August 29, 2015 14:03
Show Gist options
  • Save topher6345/13b064c2e9634bb135a9 to your computer and use it in GitHub Desktop.
Save topher6345/13b064c2e9634bb135a9 to your computer and use it in GitHub Desktop.
Common Lisp Hack night # 3

Common Lisp Hack Night # 3

Factorial recursive

(defun fact (n) 
   (if (= n 1)
       1
   (* n (fact (1- n)))))

(fact 5)

result

120

result

(fact (fact 5))

6689502913449127057588118054090372586752746333138029810295671352301633557244962989366874165271984981308157637893214090552534408589408121859898481114389650005964960521256960000000000000000000000000000

Let

A Hypotenuse function

(defun hyp (a b) 
  (let ((a2 (* a a))
        (b2 (* b b)))
    (expt (+ a2 b2) 0.5)))

Closure

 (let ((odo 0))
    (defun readodo ()
       odo)
    (defun incodo ()
    (setq odo (1+ odo))))

(readodo)

result

0

(incodo)

1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment