Hei,
I started reading "Essentials of Programming Languages" by Friedman,
Wand and Haynes. I wanted to implement the examples and the source of
the book in in CL, mainly because I like the
Emacs-Slime-SBCL-Combination. For SICP that was no big problem. Problem
in this case is, the book extensively uses a define-datatype macro
(which is actually quite cool) that is defined using syntax-case.
Porting define-datatype in a reasonable amount of time unfortunately is
way beyond my capabilities. Does anyone know if someone already
implemented something like define-datatype? Or has CL something similar
and I'm just too stupid to find it in the docs?
Greetings HB
lin8080 wrote:
> O-MY-GLIFE schrieb:
>
> > ;; parse : sexp -> expr
> > (defun parse (sexp)
> > (cond ((numberp sexp) sexp)
> > ((symbolp sexp) sexp)
>
> > (t (case (first sexp)
> > ((+) (add (parse (second sexp))
> > ....
> > (otherwise (app (parse (first sexp))
>
>
> Hei. A case-list inside a cond. Never saw this construct. Did that work
> well? What about time?
>
> stefan
Let see:
* (macroexpand-1 '(case x (1 'foo) (2 'bar)))
(LET ((#:G4299 X))
(DECLARE (IGNORABLE #:G4299))
(COND ((EQL #:G4299 '1) NIL 'FOO) ((EQL #:G4299 '2) NIL 'BAR)))
T
To make a long story short case expands to cond and cond expands (at
least in some implementations) to nested ifs.
So finally, all we have is a a lot of nested ifs. With the usual
performance, I guess.
P.S. BTW, the code for evaluating the recursive functions in the
"modern" version of interp is wrong. Another ematch/typecase is needed
to distinguish between closure and recursive-closure.
HB wrote:
> Does anyone know if someone already
> implemented something like define-datatype? Or has CL something similar
> and I'm just too stupid to find it in the docs?
The original Scheme code can be found here:
<http://www.cs.indiana.edu/eopl/code/interps/define-datatype.scm>
--
Jens Axel Søgaard
"HB" <·······@gmail.com> writes:
> I started reading "Essentials of Programming Languages" by Friedman,
> Wand and Haynes. I wanted to implement the examples and the source of
> the book in in CL, mainly because I like the
> Emacs-Slime-SBCL-Combination. For SICP that was no big problem. Problem
> in this case is, the book extensively uses a define-datatype macro
> (which is actually quite cool) that is defined using syntax-case.
> Porting define-datatype in a reasonable amount of time unfortunately is
> way beyond my capabilities. Does anyone know if someone already
> implemented something like define-datatype? Or has CL something similar
> and I'm just too stupid to find it in the docs?
You don't need to translate it. As often is the case, when you
translate 1000 LOC of a random language, you end with between only 10
and 100 of Common Lisp LOC.
Here you have 736 useless lines: use deftype and defstruct instead.
For example, you can write this:
(define-datatype define-datatype:test:btree define-datatype:test:btree?
(define-datatype:test:empty-btree)
(define-datatype:test:btree-node
(left define-datatype:test:btree?)
(key integer?)
(right define-datatype:test:btree?)))
as:
(deftype btree () '(or null btree-node))
(defstruct btree-node
(left nil :type btree*)
(key 0 :type integer)
(right nil :type btree*))
(defun empty-btree () nil) ; Is this really useful? Use NIL for all your empty stuff!
(typep (make-btree-node) 'btree) --> T
(typep nil 'btree) --> T
(typep 42 'btree) --> NIL
--
__Pascal Bourguignon__ http://www.informatimago.com/
This is a signature virus. Add me to your signature and help me to live
Hei
@Jens: Yeah, I already found the scheme sources, that's the way I knew
I couldn't reimplement it on the fly. I meant an implementation in
Common Lisp, should've said that explicit.
@Pascal: Works without problem for a normal recursive definition like
binary tree, you're right there. Trying some of the more advanced
examples get's ugly fast AFAICS.
@O-MY-GLIFE: yeah, that's what I was looking for.
@EVERYBODY: Thanks for your answers, I appreciate it.
Greertings HB
HB wrote:
> Hei
>
> @Jens: Yeah, I already found the scheme sources, that's the way I knew
> I couldn't reimplement it on the fly. I meant an implementation in
> Common Lisp, should've said that explicit.
You were explicit. The code weren't meant for you, but for
that CL'er that could either port it or point to a similar
construct, but didn't know EOPL.
--
Jens Axel Søgaard