Last active
December 17, 2015 02:59
-
-
Save thinkphp/5539631 to your computer and use it in GitHub Desktop.
Compare factorials iterative vs recursive plus (allfact '(1 2 3)) -> (1 2 6) in Common Lisp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;Adrian Statescu <thinkphp.ro> | |
| ;compare factorial written recursively with factorial written iteratively with prog | |
| ;i.e. 52! | |
| ;recursive | |
| (defun factorial (n) | |
| (if (= n 1) | |
| 1 | |
| (* n (factorial (- n 1))) | |
| ) | |
| ) | |
| ;iterative with prog | |
| (defun factorial2 (n) | |
| (prog ((p 1)(i 1)) | |
| again | |
| (setf p (* p i)) | |
| (setf i (+ i 1)) | |
| (if (> i n) (return p)) | |
| (go again) | |
| ) | |
| ) | |
| ;iterative with do | |
| (defun factorial3 (N) | |
| (let ((R 1)) | |
| (do ((i 1 (+ i 1) )) ((> i N) R) | |
| (setf r (* r i)) | |
| ) | |
| ) | |
| ) | |
| ;define a list and invoke factorial for each element | |
| (defun allfactorial3 (L) | |
| (mapcar #'(lambda (x)(factorial3 x)) L) | |
| ) | |
| ;define a list and invoke factorial for each element | |
| (defun allfact (L) | |
| (mapcar #'(lambda (x)(factorial2 x)) L) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment