Answer the question
In order to leave comments, you need to log in
Finding Fibonacci numbers on scheme?
What does the last line do?
I don't quite understand why there
aafter the first brackets.
(define (fib n)
(fib-iter 1 0 n))
(define (fib-iter a b count)
(if (= count 0)
b
(fib-iter (+ a b) a (- count 1))))
Answer the question
In order to leave comments, you need to log in
in the third line: define (fib-iter ab count)
the fib-iter function is defined as a function of 3 arguments: i element, i-1 element and a counter, which determines the end of the recursion call.
in the last line, it is called with three arguments:
first argument: (+ ab)
second argument: a
third argument: (- count 1)
that is, in the last line, the function calls itself by
substituting the sum of a and b
instead of b element a
and passes as the third argument the counter reduced by 1
....
and until the counter becomes 0, it will call itself, decrementing the counter
as soon as the counter becomes 0 - the recursion will stop - the last element will be calculated - it will return to the result ... although why do they return b ? perhaps a should be returned? :-)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question