[plt-scheme] Using eval in modules
On Jun 23, 2007, at 10:30 AM, Jean-Pierre Lozi wrote:
> 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?
My thought was that perhaps the "one generating the message" could
simply provide a reference to the appropriate function, rather than
encoding it as a string. Clearly, if the message is coming over a
TCP/IP socket, though, that's not an option.
All the best,
John Clements
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2223 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20070623/079102b4/attachment.p7s>