Fibonacci in scheme
scheme吧
全部回复
仅看楼主
level 1
somaniya 楼主
(define (fib x) (if (< x 2) x (+ (fib (-1+ x)) (fib (- x 2)))))(define (fib2 x) (define (fib-call i fibonacci-of-i-before fibonacci-of-i x-to-reach) (cond ((>= i x-to-reach) fibonacci-of-i) ((< i x-to-reach) (fib-call (1+ i) fibonacci-of-i (+ fibonacci-of-i-before fibonacci-of-i) x-to-reach)))) (cond ((< x 2) x) (#t (fib-call 1 0 1 x))))
2008年10月17日 14点10分 1
level 0
不用另外讨论(< x 2)的,可以这么改写
(define (fib n)
     (define (fib-call a b m n)
       (cond ((<= m n) (fib-call (+ a b) a (+ m 1) n))
             (else a)))
     (fib-call 0 1 1 n))
2010年08月03日 02点08分 3
(define fib (lambda (a b) (stream-cons a (fib b (+ a b))) ))
2014年04月14日 13点04分
(fib 1 1)就是你要的fibonacci 流
2014年04月14日 13点04分
1