Skip to content

Instantly share code, notes, and snippets.

@kosh04
Last active December 17, 2015 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosh04/5655831 to your computer and use it in GitHub Desktop.
Save kosh04/5655831 to your computer and use it in GitHub Desktop.
ラムダ式の内部にデータを格納したフィボナッチ計算 関数に名前がないと値の参照ができないので実用的ではない
#!/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