Assignment 8: Registerization
Show pagesource
Log In
(define ack
(lambda (m n k)
(cond
[(zero? m) (k (add1 n))]
[(zero? n) (ack (sub1 m) 1 k)]
[else (ack m (sub1 n) (lambda (v) (ack (sub1 m) v k)))])))
(define depth
(lambda (ls k)
(cond
[(null? ls) (k 1)]
[(pair? (car ls))
(depth (car ls)
(lambda (l)
(depth (cdr ls)
(lambda (r)
(let ((l (add1 l)))
(if (< l r) (k r) (k l)))))))]
[else (depth (cdr ls) k)])))
> (ack 2 2 (empty-k))
7
> (depth ‘(1 (2 (3 (4)))) (empty-k))
4
should not
> (ack-reg-driver 2 2)
7
> (depth-reg-driver ‘(1 (2 (3 (4)))))
4
.meht eziretsiger llits dluohs uoy tub ,krowemoh ruoy rof deriuqer ton era yehT .serudecorp erom owt era ereH oslA
.snoitaunitnoc fo noitatneserper larutcurts-atad a esu tsum uoY .snoitaunitnoc ot tcepser htiw tnednepedni-noitatneserper eb tsum snoitinfied llA
:dnim ni stniop gniwollof eht peek ,tnemngissa eht od uoy sA
.smargorp deziretsiger ruoy enilopmart ot deen t’nod uoy ,niaga ecnO .margorp deziretsiger eht llac dna seulav laitini htiw sretsiger eht etalupop ot eb lliw revird eht fo boj ehT .margorp deSPC-erp ,lanigiro eht yb nekat stnemugra eht ekat lliw smargorp revird eseht fo hcaE .swollof sa dekovni eb ot ,snoitcnuf revird tcurtsnoc osla dluohs uoY .serudecorp eseht enilopmart uoY .lanigiro eht fo noisrev deziretsiger a etirw dluohs uoy ,smargorp laitini owt eht fo hcae roF
:serudecorp eseht llac ot woh fo selpmaxe era ereH
.bal ro erutcel morf uoy ot detubirtsid selfi yna dna seton ssalc ruoy tlusnoc osla oD .gnizinilopmart dna noitaziretsiger no setoN
.lufpleh secruoser gniwollof eht lufesu ti dnfi yam uoy ,tnemngissa siht htiw deecorp uoy sA etoN
reverof ti no evil nac uoy taht yhsuc os ton tub ,dnuorg eht tih uoy erofeb uoy hctac ot hguone gnorts si enilopmart A
:serudecorp owt gniwollof eht redisnoC tnemngissA
(define fact
(lambda (n k)
((lambda (fact k)
(fact fact n k))
(lambda (fact n k)
(cond
[(zero? n) (k 1)]
[else (fact fact (sub1 n) (lambda (v) (k (* n v))))]))
k)))
(define pascal
(lambda (n k)
(let ((pascal
(lambda (pascal k)
(k (lambda (m a k)
(cond
[(> m n) (k ‘())]
[else (let ((a (+ a m)))
(pascal pascal (lambda (f) (f (add1 m) a (lambda (v) (k (cons a v)))))))]))))))
(pascal pascal (lambda (f) (f 1 0 k))))))
> (fact 5 (empty-k))
120
> (pascal 10 (empty-k))
(1 3 6 10 15 21 28 36 45 55)
> (fact-reg-driver 5)
120
> (pascal-reg-driver 10)
(1 3 6 10 15 21 28 36 45 55)
rampoline
rampoline fib
(define fib
(lambda (n)
(cond
[(and (not (negative? n)) (< n 2)) n]
[else (+ (fib (sub1 n)) (fib (sub1 (sub1 n))))])))
(define fib-ramp-driver
(lambda (n1 n2 n3)
(let/cc jumpout
(rampoline
(lambda ()
(fib n1 (ramp-empty-k jumpout)))
(lambda ()
(fib n2 (ramp-empty-k jumpout)))
(lambda ()
(fib n3 (ramp-empty-k jumpout)))))))
.rewsna tsrfi eht gnidnfi retfa krow yna oder t'nod eW .etelpmoc yeht redro eht ni detsil era srewsna ehT .srewsna htob htiw tsil a kcab teg eW .enilopmart a htiw smargorp htob fo snoitucexe evaelretni llits eW
?srewsna /htob/ tnaw ew fi tahw tuB .setelpmoc taht rewsna tsrfi eht nruter nac enilopmart a ni smargorp hcus owt gnitucexe woh )enigami nac ro( nees ev'ew ,smargorp dezinilopmart owt fo snoitucexe evaelretni ot desu eb nac enilopmart A
: fo noitinfied gniwollof eht ezinilopmart dna SPC ,yltcerroc skrow taht wohs oT .pool etinfini na otni og yam serudecorp eht taht etoN .etelpmoc ot tsrfi eht fo eulav eht gninruter ,redro modnar ni meht setucexe dna ,sllac erudecorp dezinilopmart gniniatnoc sknuht eerht sekat taht erudecorp ekil-enilopmart a etirW .ylsuoenatlumis smargorp dezinilopmart elpitlum etucexe ot desu eb nac senilopmarT
:nierehw ,srewsna htob nruter ot yaw a esiveD
:bfi dezinilopmart ruoy ot revird a sa gniwollof eht esU
serudecorp eseht llac ot woh fo selpmaxe era ereH
tresseD tsuJ
resaetniarB
bi-tramp-driver
bi-trampoline bi-tramp-driver trib
(define trib
(lambda (n)
(cond
((< n 3) 1)
(else
(+ (trib (- n 3))
(trib (- n 2))
(trib (- n 1)))))))
> (bi-tramp-driver 3 4)
(3 5)
> (bi-tramp-driver 4 3)
(3 5)
> (bi-tramp-driver 6 6)
(17 17)
ikralsoj yb 80:22 50/30/0202 :defiidom tsaL ¡¤ txt.8-tnemngissa
.ezinilopmart ot redro ni eziretsiger ot deen ton od uoy ,ereh :etoN .srebmun owt htiw
nur ot tes era stset detamotuA .)]srebmun_iccanobirT#srebmun_iccanobiF_fo_snoitazilareneG/ikiw/gro.aidepikiw.ne//:sptth[ ecneuqes iccanobirt eht fo stnemele setareneg
taht( fo noisrev dezinilopmart a dna , a , a enfied ot deen lliw uoY