[plt-scheme] Using eval in modules

From: Jean-Pierre Lozi (jean-pierre at lozi.org)
Date: Sat Jun 23 13:30:00 EDT 2007

On 6/23/07, John Clements <clements at brinckerhoff.org> wrote:
>
> On Jun 23, 2007, at 10:08 AM, Jean-Pierre Lozi wrote:
>
> > Thanks for your answers,
> >
> > Actually, what I am trying to do is quite simple : at some point in
> > my program, I get different messages (strings). I am trying to call
> > the appropriate function depending on the message I received.
> >
> > Of course I could dispatch the messages using cond - calling the
> > right function for each message. But I thought it would look nicer
> > using a function :
> >
> > (define (dispatch-message message)
> >      ((let-values (((sender command args) (parse-message message)))
> >         (let ((function (string->symbol
> >                  (string-append "answer-"
> >                                 (string-downcase command) "-
> > command"))))
> >           ((eval function) sender command args)))))))
> >
> > Of course, using the hash-table approach is similar to the cond
> > approach. Such a function could be unsafe, but I could test the
> > message with member before calling eval, in order to be sure that
> > the handling function exists.
> >
> > From what you all said, I understand that it is impossible. Too
> > bad. I guess I'll have to choose the cond/hashtable approach or to
> > avoid modules :)
>
> It appears to me that you can probably get something very much like
> what you want, using macros. BUT... I'll bet you thirty dollars it
> will take less time just to build a small 'cond' table.
>
> Another question; is there a good reason that your messages are
> formatted as strings? Are they coming from the user?

They are coming from a socket. Anyway, I don't see your point : what
would be the difference if they were, say, symbols?

> John Clements
>
>
>



-- 
Jean-Pierre Lozi
http://www.lozi.org
mailto:jean-pierre at lozi.org


Posted on the users mailing list.