Last active
April 5, 2018 19:55
-
-
Save mnp/d89543ef7c5430162920f679a2c28a85 to your computer and use it in GitHub Desktop.
Elisp Fibs
This file contains 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
;; g'bye stack | |
(let () | |
(defun f (n) | |
(if (< n 2) 1 | |
(+ (f (- n 1)) (f (- n 2))))) | |
(loop for i from 0 upto 20 | |
collect (f i)) | |
) | |
;; memoized, little better | |
(let ((mem (make-hash-table))) | |
(puthash 0 1 mem) | |
(puthash 1 1 mem) | |
(defun f2 (n) | |
(let ((has (gethash n mem))) | |
(if has | |
has | |
(let ((fn (+ (f2 (- n 1)) (f2 (- n 2))))) | |
(puthash n fn mem) | |
fn)))) | |
(loop for i from 0 upto 20 | |
collect (f2 i)) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment