slides-article

slides-article - (define-datatype bintree bintree?...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
(define-datatype bintree bintree? (leaf-node (datum number?)) (interior-node (key symbol?) (left bintree?) (right bintree?))) > (define bt (interior-node ’a (interior-node ’b (leaf-node 1) (leaf-node 2)) (leaf-node 3))) > bt (interior-node a (interior-node b (leaf-node 1) (leaf-node 2)) (leaf-node 3)) > (bintree? bt) #t > (bintree? 35) #f (define leaf-sum (lambda (tree) (cases bintree tree (leaf-node (n) n) (interior-node (s bt1 bt2) (+ (leaf-sum bt1) (leaf-sum bt2)))))) > bt (interior-node a (interior-node b (leaf-node 1) (leaf-node 2)) (leaf-node 3)) > (leaf-sum bt) 6 h s-list i ::= () ::= ( h sym-exp i · h s-list i ) h sym-exp i ::= h symbol i | h s-list i (define-datatype s-list s-list? 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
(empty-s-list) (non-empty-s-list (first symbol-exp?) (rest s-list?))) ;;;; Note mutual recursion. (define-datatype symbol-exp symbol-exp? (symbol-symbol-exp (data symbol?)) (s-list-symbol-exp (data s-list?))) BNF generates concrete syntax for a language, e.g. h Λ exp i ::= h id i ::= (lambda ( h id i ) h Λ exp i ) ::= ( h Λ exp i h Λ exp i ) This specifies how to write down expressions. But occurrences of “(“ and “)” are irrelevant to abstract structure. ;;; Just the essential information. (define-datatype expression expression? (var-exp (id symbol?)) (lambda-exp (id symbol?) (body expression?)) (app-exp (rator expression?) (rand expression?))) ;;; Compare to previous version. (define occurs-free? (lambda (var exp) (cases expression exp (var-exp (id) (eqv? id var)) (lambda-exp (id body) (and (not (eqv? id var)) (occurs-free? var body))) (app-exp (rator rand) (or (occurs-free? var rator) (occurs-free? var rand)))))) 2
Background image of page 2
(define unparse-expression (lambda (exp) (cases expression exp (var-exp (id) id) (lambda-exp (id body) (list ’lam (list id) (unparse-expression body))) (app-exp (rator rand) (list (unparse-expression rator) (unparse-expression rand)))))) (define parse-expression (lambda (datum) (cond ((symbol? datum) (var-exp datum)) ((pair? datum) (if (eqv? (car datum) ’lam) ;; "Special form" (lambda-exp (caadr datum) (parse-expression (caddr datum))) (app-exp (parse-expression (car datum)) (parse-expression (cadr datum))))) (else (eopl:error ’parse-expression "Invalid concrete syntax ˜s" datum)))))
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/05/2008 for the course CSE 431 taught by Professor Card during the Spring '08 term at Illinois Tech.

Page1 / 11

slides-article - (define-datatype bintree bintree?...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online