Exploring metalangs: #racketlang vs Logo

Inspired by Schorre’s Metta II, I’m exploring the idea of writing a metacompiler.

DrRacket should be top notch for this, but it’s proving to be frustrating.

One question is: should the grammar description be a “procedure” (i.e. a normal define), a macro, or a list?

I’ve ruled out the first option, as a procedure will eagerly evaluate its arguments. The grammar must be evaluated lazily, as it drives what to do.

To make it more concrete, suppose I have a rule which is defined as a foo followed by another foo. Using a list-based approach, this is fairly easy:

(define rule '(seq foo foo))

In this method of formulation, seq should be a function which evaluates each foo in turn. I need to turn seq into a function from a symbol, and eventually likewise for foo.

To further simplify things, suppose I define foo as:

(define (foo) (display "x"))

To execute foo as  s symbol, I would then want to do

(eval 'foo)

Except I can’t do that. Racket has the notion of namespaces, and eval must happen within the context of a namespace. Which means that foo needs to be defined for some namespace. Aargh. At least I think that’s part of what the problem is, anyway.

Maybe if seq was defined as a syntax rule which evaluated the car of a list passed to it and returned the cdr of the rest of the list. It could then iterate through each element in turn. That would give me my lazy evaluation. Except figuring out the exact details of that is causing a lot of problems, too.

No compare this with Logo. I define foo in a similar way:

to foo
print "|hello from foo|
end

Now, suppose I have the list [foo foo]. How do I evaluate the first foo in the list? Simple:

apply (first [foo foo]) []

It prints the expected result:

hello from foo

Success! I now have a way of iterating over a sequence.

And remember, this is really only my second day with fooling around with Logo, and I managed to achieve what I wanted. I’m still trying to futz around with getting a similar thing working in Racket.

No doubt an experienced Racketeer would have this fairly simple idea working within a minute. I’m actually encouraged by the whole Logo experience, and want to pursue it further down the rabbit-hole.

Racket, on the other hand, seems a like lot of effort for little return. Racket is a very pure language that cares a lot about correctness. In contrast to a language like Perl 😉

So, score so far: Logo 1 – Racket 0

Cue the sound of the interwebs turning on their flamethrowers.

Advertisement

About mcturra2000

Computer programmer living in Scotland.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s