[plt-scheme] Re: Novice question: evaluating symbols

From: mike at goblin.punk.net (mike at goblin.punk.net)
Date: Fri Jan 8 08:55:04 EST 2010

On Thu, Jan 07, 2010 at 08:57:46AM -0600, Robby Findler wrote:
> 
> The simplest way to rewrite the below would be to make a table mapping
> those symbols to the corresponding functions. I'm going to guess that
> you don't like that, tho, because you want to avoid writing both
> "year" and "date-year". The better way to do this would be to use a
> macro that, at compile time, constructs the identifiers that you are
> after and then builds code for the table, for use at runtime. (This
> approach is well-supported by our tools.)

Thank you for the explanation.  At the time, I was just
exploring what I could do with Scheme.  I am still trying to
think Scheme-ishly.  I haven't learned macros yet, but maybe
that should move up on my agenda.  

Scheme seems to have more levels of indirection than what I'm
used to.  Returning to my (contrived) problem, how might I force
the evaluation of a symbol?  For example, the apply expression
below will evaluate date-year and then apply the procedure to
a-date, but the subsequent map expression will get the symbol
'date-year and try to apply that to a-date.

Or is this approach a dead end in Scheme? 

(define selectors
  (map (λ (a-symbol) (string->symbol (string-append "date-" (symbol->string a-symbol))))
       '(year 
         month 
         day 
         week-day 
         hour 
         minute 
         second)))

(apply date-year (list a-date))

(map (λ (x) (apply x (list a-date)))
     selectors)

Thanks again,
MJG

P.S.  I've changed my subscription details to try to avoid
triggering the spam filter.


Posted on the users mailing list.