[plt-scheme] Wrote some notes on using PLT Scheme for TAPL stuff

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Wed Jan 4 17:42:24 EST 2006

> I've been reading Pierce's excellent "Types and Programming Languages"
> textbook, and thought it might be fun to try to use PLT Scheme instead of
> OCaml.  It's also motivating me to learn the parser-tools collection.
> I've written some notes I hope folks find useful.  So far, the notes show
> introductory usage of plt-match.ss, structures, and the parser-tools to
> implement the baby interpreter in Chapter Four.

It looks interesting. You had a footnote about the datatype definition
in EOPL. It is quite easy to use. Here is example with an unbalanced
binary tree of integers:

(require (lib "datatype.ss" "eopl"))

(define-datatype <tree> tree?
   (empty)
   (tree (left    tree?)
         (element integer?)
         (right   tree?)))

(define (insert x t)
   (cases <tree> t
     (empty ()      (tree (empty) x (empty)))
     (tree  (l y r) (cond
                      [(< x y) (tree (insert x l) y r)]
                      [(> x y) (tree l y (insert x r))]
                      [else    (tree l y (tree (empty) x r))]))))

(define (tree->list t)
   (cases <tree> t
     (empty ()     '())
     (tree (l x r) (append (tree->list l)
                           (list x)
                           (tree->list r)))))

(define (sort xs)
   (tree->list (foldl insert (empty) xs)))

(sort (list 9 2 3 7 1 5 4 6 8))


Btw: Matthew: Here is an idea for more better error/warning
checking in the define-datatype macro:

The faulty definition

(define-datatype tree tree?
   (empty)
   (tree (left    tree?)
         (element integer?)
         (right   tree?)))

does not provoke a warning. However since the constructor shadows the 
type, one gets a confusing error later on :

(define (tree->list t)
   (cases tree t
     (empty ()     '())
     (tree (l x r) (append (tree->list l)
                           (list x)
                           (tree->list r)))))

gives the error

   "cases: not a datatype name in: tree"

which is true, since it is shadowed by the constructor - but ...
I'd rather have had the warning when I used define-datatype.

-- 
Jens Axel Søgaard




Posted on the users mailing list.