lisp - Creating a list of Fibonacci numbers in Scheme? -
i've made basic program output fibonacci sequence whatever length "n". here's code have:
(define (fibh n)   (if (< n 2)       n       (+ (fibh (- n 1)) (fibh (- n 2)))))  (define (fib n)   (do ((i 1 (+ 1)))       ((> n))     (display (fibh i))))   it output, example, 112358.
what want list such (1 1 2 3 5 8).
any explanation how appreciated.
(map fibh '(1 2 3 4 5 6))   would trick. if don't want enumerate integers hand, implement simple recursive function you, like:
(define (count n)   (if (= n)     '()     (cons (count (+ 1) n))))   (note: not tail-recursive, algorithm compute fibonacci numbers, that's not prime concern.)
Comments
Post a Comment