Last active
May 29, 2017 21:32
-
-
Save cleac/0e9536b9c3cc867e9dabbd0c611f1395 to your computer and use it in GitHub Desktop.
A factorial implementation in 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
; More functional approach, but it gets a stack overflowed on huge numbers | |
(defun factorial(num) | |
(cond | |
((< num 1) 1) | |
(t (* (factorial (- num 1)) num)) | |
) | |
) | |
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
(defun factorial(num) | |
(let ((result 1)) | |
(loop | |
(setq result (* result num)) | |
(setq num (- num 1)) | |
(when (< num 1) (return result)) | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment