The language for this homework is:
#lang plait |
Roughly speaking, the Continuation Passing Style (CPS) takes a function call like
(...stuff... (f ...args...) ...more-stuff...) |
(f/k ...args... (lambda (<*>) (...stuff... <*> ...more-stuff...))) |
To get started, let’s add support for a single argument decrement function. I’ve written it here as --, even though it doesn’t mutate like -- does in most languages.
In this "inside out" style, we need to call interp on the argument, with a last argument of "what to do next". The function ‘continue‘ will then handle processing the evaluated argument.
(test (parse `{-- 2}) (Sub1 (Num 2)))) |
(test (run `{-- 2}) (NumV 1)) (test (run `{{lam {x} {-- x}} 3}) (NumV 2)) (test (run `{{lam {y} {+ {-- y} {-- y}}} 10}) (NumV 18)) (test (run `{{lam {f} {f 4}} {lam {x} {-- x}}}) (NumV 3)) |
(define fact-prog `{{lam {mkrec} {{lam {fact} ;; Call fact on 5: {fact 5}} ;; Create recursive fact {mkrec {lam {fact} {lam {n} {if0 n 1 {* n {fact {-- n}}}}}}}}} ;; mkrec: {lam {body-proc} {{lam {fX} {fX fX}} {lam {fX} {body-proc {lam {x} {{fX fX} x}}}}}}}) (test (run fact-prog) (NumV 120)) |
Ensure you have complete test coverage.
Define minutes-spent as the number of minutes you spent on your homework.