I'm trying to implement generators to make a list of fibonacci numbers in Scheme, but i can't do it. I have two functions, the first is a function that returns the Fibonacci numbers in the form of a list and the second is the generator function.
What I have to do is finally transform the Fibonacci function into a generator from a list of Fibonacci numbers.
;FIBONACCI NUMBERS
(define (fib n a b i)
(if
(= i n)
(list b)
(cons b (fib n b (+ a b) (+ i 1)))
)
)
(define (fibonacci n)
(cond
((= n 1) (list 1))
(else (fib n 0 1 1))
)
)
;GENERATOR
(define (generator start stop step)
(let ((current (- start 1)))
(lambda ()
(cond ((>= current stop) #f)
(else
(set! current (+ current step))
current)))))
(define (next generator)
(generator))