Last active
October 9, 2015 05:18
-
-
Save alcidesfp/3445063 to your computer and use it in GitHub Desktop.
Función para calcular el enésimo término de la sucesión de Fibonacci en Scheme
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Versión iterativa compacta con DO | |
(define (fib n) | |
(do ((a 0 b)(b 1 (+ a b))(i n (- i 1))) | |
((= i 0) a ))) | |
;; Versión iterativa explícita con named LET | |
(define (fib n) | |
(let loop ((a 0) | |
(b 1) | |
(i n)) | |
(if (= i 0) | |
a | |
(loop b (+ a b) (- i 1)) ))) | |
;; Referencias: | |
;; http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.2 | |
;; http://rosettacode.org/wiki/Fibonacci_sequence#Scheme |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment