From 6e4e2f8f789c145b7e76eee4509054fbd9eb4414 Mon Sep 17 00:00:00 2001 From: Alia Bellamy Date: Thu, 19 Nov 2020 22:00:31 -0500 Subject: [PATCH] nested let --- Chp4/4.7-standalone.rkt | 22 +++++++++++++++++++- Chp4/4.7-standalone.rkt~ | 45 ++++++++++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/Chp4/4.7-standalone.rkt b/Chp4/4.7-standalone.rkt index a6ce4da..cba87c7 100644 --- a/Chp4/4.7-standalone.rkt +++ b/Chp4/4.7-standalone.rkt @@ -63,6 +63,26 @@ (let ((list-wrap (map list list-of-expressions))) (cons 'let (list (map cons list-of-vars list-wrap) body)))) +(define (make-single-let list-of-vars list-of-expressions body) + (list 'let (list (list (car list-of-vars) (car list-of-expressions))) body)) + (define ltest (make-let '(x y )'((+ 3 4) (- 3 2)) '(* x y))) (let->combination ltest) -(let*->nested ltest) \ No newline at end of file +(let*->nested ltest) + +(define (let*->nested-lets let-block) + (define (let*-iter real-body vars exps) + (if (last-var? vars) + (make-single-let vars exps (car real-body)) + (make-single-let vars exps (let*-iter real-body (cdr vars) (cdr exps))))) + (let*-iter (let-body let-block) + (let-variables let-block) + (let-expressions let-block))) +(newline) +(let*->nested-lets let3) +(newline) +(let->combination (let*->nested-lets let3)) +(newline) +(let*->nested-lets ltest) +(newline) +(let->combination (let*->nested-lets ltest)) \ No newline at end of file diff --git a/Chp4/4.7-standalone.rkt~ b/Chp4/4.7-standalone.rkt~ index 1f1102c..a6ce4da 100644 --- a/Chp4/4.7-standalone.rkt~ +++ b/Chp4/4.7-standalone.rkt~ @@ -33,23 +33,36 @@ (define let1 '(let ((x (+ 5 5)))(+ x 1))) -(define let2 '(let ((x (+ free 2)) - (y (* free2 free3))) (+ 1 x y))) +(define let2 '(let ((x (+ 1 2)) + (y (* x 2))) + (+ 1 x y))) -(define (last-var? vars) (null? (cdr vars))) +(define let3 '(let* ((w 3) + (x w) + (y (+ x 2)) + (z (+ x y 5))) + (* x z))) -(define (let*->combination let-block) +(define (last-var? vars) (null? (cdr vars))) +(define (let*->nested let-block) (define (let*-iter real-body vars exps) - (if (last-var? vars) - (cons (make-lambda vars real-body) exps) - (cons (make-lambda (list (car vars)) - (let*-iter real-body (cdr vars) (cdr exps))) - (list (car exps))))) - - (let ((var-list (let-variables let-block)) - (exps-list (let-expressions let-block)) - (body (let-body let-block))) - (let*-iter body var-list exps-list))) - - \ No newline at end of file + (if (null? vars) + (list (list (make-lambda vars real-body))) + (list (list (make-lambda (list (car vars)) + (let*-iter real-body (cdr vars) (cdr exps))) + (car exps))))) + (car (let*-iter + (let-body let-block) + (let-variables let-block) + (let-expressions let-block)))) + +(let*->nested let3) + +(define (make-let list-of-vars list-of-expressions body) + (let ((list-wrap (map list list-of-expressions))) + (cons 'let (list (map cons list-of-vars list-wrap) body)))) + +(define ltest (make-let '(x y )'((+ 3 4) (- 3 2)) '(* x y))) +(let->combination ltest) +(let*->nested ltest) \ No newline at end of file