[plt-scheme] Re: How to use a custom procedure as read?

From: Ali (emailaliclark at gmail.com)
Date: Tue Sep 30 09:37:28 EDT 2008

On Sep 30, 1:12 pm, "Noel Welsh" <noelwe... at gmail.com> wrote:
> On Tue, Sep 30, 2008 at 3:45 AM, Ali <emailalicl... at gmail.com> wrote:
> >> > (swap-the-reader-in)
> >> > ... code using custom reader ...
> >> > (swap-the-reader-out)
> >> > ... normal reader again ...
> ...
> > Thanks. Do I have to write the code in a module? I'd much rather be
> > able to swap in and out of the custom reader mode as with above.
>
> You have to write your code in a separate module.  If you did not,
> Scheme would have to evaluate your code as the module was loaded to
> work out that you were swapping in a new reader.  This would break a
> lot of things, the module system being the prime one.

Okay, fair enough. So to take this example of a module named canvas,
how could I modify it to specify that to use my custom-reader?

(module canvas mzscheme

  ... code implementing canvas using custom-reader as the reader ...

  (provide new-canvas draw-dot! display-canvas))

>
> Note the documentation you are reading is for v4, while you're using
> v3.  I suggest you upgrade your installation.

Thanks. I upgraded Mzscheme. Tried a few things, but I've still got no
idea what code I have to run, do I need a file in a certain place etc.
Can't really remember the last time I was this confused, so at least
its an experience for me!)

>
> Finally, writing a custom reader is a fairly advanced task.  What is
> the problem you're trying to solve?  Perhaps list members can suggest
> a simpler way of solving it.

I'd like to try writing code using SRFI 49 (http://srfi.schemers.org/
srfi-49/srfi-49.html). With a couple of minor changes I can run the
example implementation in guile, using (setf! read sugar-read). Works
fine. With a few more changes, Mzscheme can load the reader. All I
need now is the Mzscheme equivalent for (setf! read sugar-read), which
apparently doesn't do anything in mzscheme.

Sorry for not saying this explicitly before, but I was concerned the
discussion would be side-tracked to reasons why I shouldn't be using
SRFI 49 in the first place.

Anyway, thanks for telling me about the docs version, and making it
clear that I need to use a module. Really hope someone can give just
the smallest bit of example usage, just an equivalent for (set! read
sugar-read), where sugar-read is a user defined procedure. Or point me
to an existing example usage. Sorry if my irritation shows, I am
actually very grateful for your time, and I know there are more
enjoyable things one can do with it!


Posted on the users mailing list.