Skip to content

Commit

Permalink
ch4
Browse files Browse the repository at this point in the history
  • Loading branch information
slobodin committed Aug 27, 2012
1 parent 5956bce commit c70a3a7
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 1 deletion.
32 changes: 31 additions & 1 deletion ch4/ex4-2.scm
Expand Up @@ -2,4 +2,34 @@

;; Example 4.2

;; trying to apply (define) func. But define is the special case.
;; a. trying to apply (define) func. But define is the special case.

;; b.

(define (eval exp env)
(cond ((self-evaluating? exp) exp)
((variable? exp) (lookup-variable-value exp env))
((quoted? exp) (text-of-quotation exp))
((application? exp)
(apply (eval (operator exp) env) ;; apply procedure (proc, args)
(list-of-values (operands exp) env)))
((assignment? exp) (eval-assignment exp env))
((definition? exp) (eval-definition exp env))
((if? exp) (eval-if exp env))
((lambda? exp)
(make-procedure (lambda-parameters exp)
(lambda-body exp)
env))
((begin? exp)
(eval-sequence (begin-actions exp) env))
((cond? exp) (eval (cond->if exp) env))
(else
(error "Unknown expression type -- EVAL" exp))))

;; procedure invocation
(define (application? exp) (tagged-list? exp 'call))
(define (operator exp) (cadr exp))
(define (operands exp) (cddr exp))
(define (no-operands? ops) (null? ops))
(define (first-operand ops) (car ops))
(define (rest-operands ops) (cdr ops))
1 change: 1 addition & 0 deletions ch4/ex4-3.scm
@@ -0,0 +1 @@
#lang racket
47 changes: 47 additions & 0 deletions ch4/ex4-4.scm
@@ -0,0 +1,47 @@
#lang racket

;; Example 4.4

;; not sure need testing

(define (eval exp env)
(cond ((self-evaluating? exp) exp)
((variable? exp) (lookup-variable-value exp env))
((quoted? exp) (text-of-quotation exp))
((assignment? exp) (eval-assignment exp env))
((definition? exp) (eval-definition exp env))
((if? exp) (eval-if exp env))

((and? exp) (eval-and (and-operands exp) env))
((or? exp) (eval-or (or-operands exp) env))

((lambda? exp)
(make-procedure (lambda-parameters exp)
(lambda-body exp)
env))
((begin? exp)
(eval-sequence (begin-actions exp) env))
((cond? exp) (eval (cond->if exp) env))
((application? exp)
(apply (eval (operator exp) env) ;; apply procedure (proc, args)
(list-of-values (operands exp) env)))
(else
(error "Unknown expression type -- EVAL" exp))))

;; and

(define (and? exp) (tagged-list? exp 'and))
(define (and-operands exp) (cdr exp))
(define (eval-and exp env)
(cond ((null? exp) 'true)
((false? (eval (car exp))) 'false)
(else (eval-and (cdr exp) env))))

;; or

(define (or? exp) (tagged-list? exp 'or))
(define (or-operands exp) (cdr exp))
(define (eval-or exp env)
(cond ((true? (eval (car exp))) 'true)
((null? exp) 'false)
(else (eval-or (cdr exp) env))))
55 changes: 55 additions & 0 deletions ch4/ex4-5.scm
@@ -0,0 +1,55 @@
#lang racket

;; Example 4.5

;; TODO

(define (eval exp env)
(cond ((self-evaluating? exp) exp)
((variable? exp) (lookup-variable-value exp env))
((quoted? exp) (text-of-quotation exp))
((assignment? exp) (eval-assignment exp env))
((definition? exp) (eval-definition exp env))
((if? exp) (eval-if exp env))
((lambda? exp)
(make-procedure (lambda-parameters exp)
(lambda-body exp)
env))
((begin? exp)
(eval-sequence (begin-actions exp) env))
((cond? exp) (eval (cond->if exp) env))
((application? exp)
(apply (eval (operator exp) env) ;; apply procedure (proc, args)
(list-of-values (operands exp) env)))
(else
(error "Unknown expression type -- EVAL" exp))))

(define (cond? exp) (tagged-list? exp 'cond))
(define (cond-clauses exp) (cdr exp))
(define (cond-else-clause? clause)
(eq? (cond-predicate clause) 'else))
(define (cond-predicate clause) (car clause))
(define (cond-actions clause) (cdr clause))
(define (cond->if exp)
(expand-clauses (cond-clauses exp)))

(define (expand-clauses clauses)
(if (null? clauses)
'false
(let ((first (car clauses))
(rest (cdr clauses)))
(if (cond-else-clause? first)
(if (null? rest)
(sequence->exp (cond-actions first))
(error "Clause else not the last" clauses))
(make-if (cond-predicate first)
(sequence->exp (cond-actions first))
(expand-clauses rest))))))

(define (last-exp? seq) (null? (cdr seq)))
(define (first-exp seq) (car seq))
(define (make-begin seq) (cons 'begin seq))
(define (sequence->exp seq)
(cond ((null? seq) seq)
((last-exp? seq) (first-exp seq))
(else (make-begin seq))))

0 comments on commit c70a3a7

Please sign in to comment.