Fibonacci sequence: Difference between revisions

added scheme
(added scheme)
Line 485:
fib_rec (n - 1) + fib_rec (n - 2)</ocaml>
 
The previous way is the naive form, because for most n the fib_rec is called twice, and it is not tail recursive because it adds the result of two function calls. The next version resolves these problems: <!-- but of course tail recursion is effectively iteration -->
 
<ocaml>
Line 609:
else:
return fibRec(n-1) + fibRec(n-2)</python>
 
=={{header|Scheme}}==
===Iterative===
<scheme>(define (fib-iter n)
(do ((num 2 (+ num 1))
(fib-prev 1 fib)
(fib 1 (+ fib fib-prev)))
((>= num n) fib)))</scheme>
 
===Recursive===
<scheme>(define (fib-rec n)
(if (<= n 2)
1
(+ (fib-rec (- n 1))
(fib-rec (- n 2)))))</scheme>
 
=={{header|Tcl}}==
Anonymous user