Re: How to find a procedure given a symbol or string

Subject: Re: How to find a procedure given a symbol or string
From: Brandon Ibach <bibach@xxxxxxxxxxxxxx>
Date: Tue, 27 Jul 1999 19:58:08 -0500
Quoting Mitch C. Amiano <amiamc@xxxxxxxxxxxxxxx>:
> A question for the old-time DSSSLers out there: is there any way to 
> produce an existing procedure object when given a string or a symbol?
> I want to use an attribute value to determine a function to be called,
> and want to avoid lots of "if"s or a "case". 
> 
> That is, given tags like
> 
> <jediknight   name="obiwan" func="apprentice">
> <jediknight   name="maul" func="apprentice">
> <jediknight   name="anakin" func="badactor">
> 
> and a DSSSL functions
> 
> (define (apprentice name) (string-append name " is an apprentice" ))
> (define (badactor name) (string-append name " needs more work" )) 
> 
> I want to use the value of "func" to call the correct 
> DSSSL function, something like what would be 
> expressed as "call-some-function-using-a-string" in
> the following snippet:
> 
> (element JEDIKNIGHT
>    (let ( (func (attribute-string "FUNC") ) 
>            (name (attribute-string "NAME") ) )
>         (literal  (call-some-function-using-a-string func name) ) ) )
> 
   So far as I have thought this through, I don't believe it is
possible.  In order to pull it off, you'd need to produce a symbol
(easy enough with (string->symbol)), and then retrieve the value (in
this case, a procedure) that is bound to it.  I've yet to come up with
a way to do that last part, short of some sort of (eval) construct,
which DSSSL doesn't have, so far as I'm aware.
   However, here's a compromise:

(define defproc apprentice)
(define procmap `(("apprentice" ,apprentice) ("badactor" ,badactor)))

(define (call-some-function-using-a-string func name)
  ((car (cdr (or (assoc func procmap) '(#f defproc)))) name))

   Having to maintain the "procmap" list might be almost as bad as
having to maintain a big (case), however.  *shrug*  Note that to use
this, you may need to include a definition for (assoc).  There's one
available from the DSSSL Procedure Library at www.mulberrytech.com.

-Brandon :)


 DSSSList info and archive:  http://www.mulberrytech.com/dsssl/dssslist


Current Thread