Created
May 11, 2012 02:24
-
-
Save fbanados/2657106 to your computer and use it in GitHub Desktop.
Auxiliar 7
This file contains 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
#lang racket | |
;P1 No. Al evaluar, el nombre 'fibs en la llamada recursiva no se encontrará en el ambiente. | |
;P3: | |
;pow no es recursiva por la cola | |
(define (pow base exp) | |
(if (= exp 0) | |
1 | |
(* base (pow base (- exp 1))))) | |
;pow2 si es recursiva por la cola | |
(define (pow2 base exp) | |
(pow-iter base exp 1)) | |
(define (pow-iter base exp result) | |
(if (= exp 0) | |
result | |
(pow-iter base (- exp 1) (* base result)))) | |
;Restringiendo la memoria de Racket a 1MB, podemos ver que (hidevalue pow) se cae y (hidevalue pow2) no (puesto que al ser recursivo por la cola, no hace crecer el stack). | |
(define (hidevalue f) | |
(begin | |
(f 2 250000) | |
#t)) | |
;P4: La hicimos en Racket por simplicidad, para aprovechar funciones multiparámetro. | |
(define (fibs n) | |
(letrec ((fibs-iter (λ (i fib-1 fib-2) | |
(if | |
(= n i) | |
fib-1 | |
(fibs-iter (add1 i) | |
(+ fib-1 fib-2) | |
fib-1)) | |
))) | |
(fibs-iter 0 1 0))) | |
;P5: Utilizando FAE: | |
(run '{with | |
(fix (fun (f) | |
(with [frec (fun (next) | |
(fun (n) | |
((f (next next)) n)))] | |
(frec frec)))) | |
{with (fact | |
(fix | |
(fun (fact) | |
(fun (x) | |
(if0 x | |
1 | |
(* (fact (+ x -1)) x)))))) | |
(fact 4)}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment