[plt-scheme] Re: Novice question: evaluating symbols
Turning symbols into identifiers at runtime is fundamentally an
eval-like thing and should be avoided in any well-designed language
(to get in my high horse for a moment).
Computing with identifiers and thier names at compile time, on the
other hand, can also be confusing and lead to some funny issues, but
at least does not require invoking your compiler at runtime and all of
that baggage.
Robby
On Friday, January 8, 2010, <mike at goblin.punk.net> wrote:
> 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.
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>