Last active
December 17, 2015 18:49
-
-
Save kosh04/5655831 to your computer and use it in GitHub Desktop.
ラムダ式の内部にデータを格納したフィボナッチ計算
関数に名前がないと値の参照ができないので実用的ではない
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
#!/usr/bin/env newlisp | |
;; 可能ならbigintを使用する | |
(when (primitive? bigint) | |
(constant '_+ + | |
'+ (lambda () (apply _+ (map bigint (args)))))) | |
; (define (fibonacci n) | |
; (if (< n 2) | |
; 1 | |
; (+ (fibonacci (- n 1)) | |
; (fibonacci (- n 2))))) | |
(define (fibonacci) | |
(cons 0 1) ; <- initial values | |
(let ((x (nth '(1 1) fibonacci)) | |
(y (nth '(1 2) fibonacci))) | |
(setf (nth '(1 1) fibonacci) y | |
(nth '(1 2) fibonacci) (+ x y)) | |
y)) | |
(dotimes (i (or (int (main-args 2)) 10)) | |
(println i "->" (fibonacci))) | |
(exit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment