Skip to content

Commit

Permalink
nested let
Browse files Browse the repository at this point in the history
  • Loading branch information
Alia Bellamy authored and Alia Bellamy committed Nov 20, 2020
1 parent 53f41d4 commit 6e4e2f8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
22 changes: 21 additions & 1 deletion Chp4/4.7-standalone.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
(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))
45 changes: 29 additions & 16 deletions Chp4/4.7-standalone.rkt~
Original file line number Diff line number Diff line change
Expand Up @@ -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)))


(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)

0 comments on commit 6e4e2f8

Please sign in to comment.