Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
150 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#lang racket | ||
|
||
;; Example 4.6 | ||
|
||
;; Need testing. | ||
|
||
(define (eval exp env) | ||
(cond (...) | ||
((let? exp) (eval (let->combination (cdr exp)) env)) | ||
(...))) | ||
|
||
;; let stuff | ||
|
||
(define (let? exp) | ||
(tagged-list? exp 'let)) | ||
|
||
(define (let-body exp) (cdr exp)) | ||
(define (let-vars-and-exps exp) (car exp)) | ||
|
||
;; should use foldr with cons '() and filter. But.... let's use simple scheme to make simple scheme interpreter | ||
(define (let-vars exp) | ||
(if (null? exp) | ||
'() | ||
(cons (caar exp) (let-vars (cdr exp))))) | ||
|
||
(define (let-exps exp) | ||
(if (null? exp) | ||
'() | ||
(cons (cadar exp) (let-vars (cdr exp))))) | ||
|
||
(define (let->combination exp) | ||
(cons (make-lambda (let-vars (let-vars-and-exps exp)) (let-body exp)) | ||
(let-exps (let-vars-and-exps exp)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#lang racket | ||
|
||
;; Example 4.7 | ||
|
||
;; Need testing. | ||
|
||
(define (eval exp env) | ||
(cond (...) | ||
((let*? exp) (eval (let*->nested-lets (cdr exp)) env)) | ||
(...))) | ||
|
||
;; let* stuff | ||
|
||
(define (let*? exp) | ||
(tagged-list? exp 'let*)) | ||
|
||
(define (let*-body exp) (cdr exp)) | ||
(define (let*-vars-and-exps exp) (car exp)) | ||
|
||
(define (let*->nested-lets exp) | ||
(define (helper var-exps body) | ||
(if (null? (cdr var-exps)) | ||
(cons 'let (cons var-exps body)) ;; final let | ||
(list 'let (list (car var-exps)) | ||
(helper (cdr var-exps) body)))) | ||
(helper (let*-vars-and-exps exp) (let*-body exp))) | ||
|
||
|
||
;; random test stuff | ||
|
||
(define initial '(let ((a 2) | ||
(b 3) | ||
(c (foo 42))) | ||
(body 2))) | ||
|
||
initial | ||
|
||
(define vars-exps (cadr initial)) | ||
(define body (cddr initial)) | ||
|
||
vars-exps | ||
body | ||
|
||
(define last-var-exp (cddr vars-exps)) | ||
(cons 'let (cons last-var-exp body)) | ||
|
||
(display "***") | ||
|
||
(car vars-exps) | ||
|
||
(list 'let (list (car vars-exps)) | ||
(cons 'let (cons last-var-exp body))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#lang racket | ||
|
||
;; Example 4.8 | ||
|
||
;; Need testing. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#lang racket | ||
|
||
;; Example 4.9 | ||
|
||
;; Need testing. | ||
|
||
;; (while (exp) | ||
;; (body))) | ||
|
||
(define (eval exp env) | ||
(cond (...) | ||
((while? exp) (eval-while exp env)) | ||
(...))) | ||
|
||
(define (while? exp) (tagged-list? exp 'while)) | ||
(define (while-cond exp) (cadr exp)) | ||
(define (while-body exp) (caddr exp)) | ||
|
||
(define (while->combination exp) | ||
(sequence->exp | ||
(list (list 'define | ||
(list 'loop) ;; (define (loop) (if () (begin (body) (loop)) 'done)) | ||
(make-if (while-cond exp) | ||
(sequence->exp (list (while-body) | ||
(list 'loop))) | ||
'done)) | ||
(list 'loop)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters