; CS334: Principles of Programming Languages ; Prof. McGuire ; ; Examples of reducing Scheme to single-argument procedures. ; Let R[] be the rewrite operator. Recursive definitions follow: ; ; APP: ; R[ (exp1 ... expn) ] => ; (...(( R[exp1] R[exp2]) R[exp3]) ... R[expn]) ; ; LAMBDA: ; R[ (lambda (a1 ... an) body) ] => ; (λ (a1) (λ (a2) ... R[body] ) ...) ; ; The above is a process called "Currying"; it is used for partial ; function evaluation as well. ; ; From here on, I'll use multiple argument procedures with the ; understanding that we could just rewrite them as single-argument ; ones. ; ; LET: ; R[ (let ([i1 exp1] [i2 exp2] ... [in expn]) body) ] => ; ((λ (i1 i2 ... in) R[body]) R[exp1] R[exp2] ... R[expn]) ; ; ; Now, we define some procedures. Their application is rewritten as ; above, so we just need definitions that only use single-argument ; procedure creation and application. ; ; Assume R6RS CONS, which constructs a pair from two values (the ; second need not be another CONS cell, as in PLT Scheme). (define (CONS a b) (λ (which) (which a b))) ; The implementations of FIRST and REST are left as exercises for the ; class. We'll complete them in the following lecture and solutions ; will be posted online. Here's a test: (define (FIRST p) ???) (define (REST p) ???) (define (SECOND p) (FIRST (REST p))) (define (THIRD p) (FIRST (REST (REST p)))) (define (assert x) (if (not x) (error 'Assertion "Assertion Failed") (void))) (assert (eq? (FIRST (CONS 1 2)) 1)) (assert (eq? (REST (CONS 1 2)) 2)) (assert (eq? (SECOND (CONS 1 (CONS 2 3))) 3))