[plt-scheme] PLAI mzscheme and drscheme question
Hi all,
I am not sure if this is the right place to ask questions about plai,
if not let me know. First off, great book I am really enjoying it,
even if I am sweating some of the stuff in there.:-)
I have run into a problem where code that drscheme is happy with,
breaks in mzscheme. I have attached the whole page of code below, but
the problem area seems to be this:
(define-type FunDef
[fundef (fun-name symbol?)
(arg-name symbol?)
(body F1WAE?)])
When I run code that uses this is:
(print "{double {double 5}}")
(interp (parse '{double {double 5}})
(list (fundef 'double
'n
(add (id 'n) (id 'n)))))
>"{double {double 5}}"
> STDIN::6857: fundef: illegal use of syntax in: (fundef (quote
double) (quote n) (add (id (quote n)) (id (quote n))))
Maybe it is something I am doing wrong, but if so I can't work out why
drscheme would be happy with it.
Here is the code that causes the error (various tests, and other
comments removed).
Thanks for any help with this, and if I am going to be asked why I am
not using drscheme I can post a mail detailing things that make
drscheme hard to use, but here did not seem the place.
rohan
_________________________________________________________________________
(require (lib "plai-advanced.ss" "plai" ))
;; Just to clarify for my muddled mind. What is below is not the
;; function definition but the use of the function definition.
;; ie. our language's equivalent of apply
(define-type F1WAE
[num (n number?)]
[add (lhs F1WAE?)(rhs F1WAE?)]
[sub (lhs F1WAE?)(rhs F1WAE?)]
[with (name symbol?) (named-expr F1WAE?) (body F1WAE?)]
[id (name symbol?)]
[app (fun-name symbol?) (arg F1WAE?)])
(define-type FunDef
[fundef (fun-name symbol?)
(arg-name symbol?)
(body F1WAE?)])
;; newest form of parse, with apply added in.
(define parse
(lambda (sexp)
(cond
[(number? sexp) (num sexp)]
[(symbol? sexp) (id sexp)]
[(list? sexp)
(case (first sexp)
[(+)(add (parse (second sexp))
(parse (third sexp)))]
[(-) (sub (parse (second sexp))
(parse (third sexp)))]
[(with) (with (first (second sexp))
(parse (second (second sexp)))
(parse (third sexp)))]
[else (app (first sexp)
(parse (second sexp)))]
)])))
;; interp: F1WAE listof(fundef) -> number
;; evaluates F1WAE expressions by reducing them to their corresponding values
(define interp
(lambda (expr fundefs)
(type-case F1WAE expr
[num (n) n]
[add (l r) (+ (interp l fundefs) (interp r fundefs))]
[sub (l r) (- (interp l fundefs) (interp r fundefs))]
[with (bound-id named-expr bound-body)
(interp (subst bound-body
bound-id
(num (interp named-expr fundefs)))
fundefs)]
[id (v) (error 'interp "Free identifier")]
[app (fun-name arg-expr)
(local ([define the-fun-def (lookup-fundef fun-name fundefs)])
(interp (subst (fundef-body the-fun-def)
(fundef-arg-name the-fun-def)
(num (interp arg-expr fundefs)))
fundefs))])))
;; helper functions
(define lookup-fundef
(lambda (fun-name fundefs)
(cond
[(empty? fundefs)
(error 'lookup-fundef "Function does not exist")]
[else (if (symbol=? fun-name (fundef-fun-name (first fundefs)))
(first fundefs)
(lookup-fundef fun-name (rest fundefs)))])))
(define subst
(lambda (expr sub-id val)
(type-case F1WAE expr
[num (n) expr]
[add (l r) (add (subst l sub-id val)
(subst r sub-id val))]
[sub (l r) (sub (subst l sub-id val)
(subst r sub-id val))]
[with (bound-id named-expr bound-body)
(if (symbol=? bound-id sub-id)
(with bound-id
(subst named-expr sub-id val)
bound-body)
(with bound-id
(subst named-expr sub-id val) ;; added to take care of scope
(subst bound-body sub-id val)))]
[id (v)(if (symbol=? v sub-id) val expr)]
[app (fun-name arg-expr)
(app fun-name (subst arg-expr sub-id val))])))
;; test
(print "{double {double 5}}")
(interp (parse '{double {double 5}})
(list (fundef 'double
'n
(add (id 'n) (id 'n)))))
;; this yields this error in mzscheme
;; > STDIN::6857: fundef: illegal use of syntax in: (fundef (quote
;; double) (quote n) (add (id (quote n)) (id (quote n))))
___________________________________________________________________________________