[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: advice on how to use syntax transformers

From: Panicz Maciej Godek
Subject: Re: advice on how to use syntax transformers
Date: Fri, 6 Mar 2015 16:54:17 +0100

2015-03-06 15:51 GMT+01:00 Federico Beffa <address@hidden>:

I'm writing to ask for help in understanding syntax
transformers. Specifically, I'm trying to construct a function of the
following form

(define (key->value meta)
  (match meta
    (() '())
    (((("name") value) rest ...)
    (((k value) rest ...)
     (key->value (cdr meta)))
    (_ "key Not fount")))

where I would like the ability to program the actual form of the ("name")
pattern with a second function argument.

I think that the following code (without additional syntax transformers) should work for you:

(define (key->value meta key)
  (match meta
    (() '())
    ((((? (lambda(x) (equal? x key))) value) rest ...)
    (((key value) rest ...)
     (key->value (cdr meta)))

but I don't know if that answers your question.

It seems to me that you are trying to use the value of a function argument to construct a macro. Even if you managed to make it run somehow (which should be possible through some nasty hacks), it would be an abuse, because macros are intended to be used during macro expansion time, and not during runtime (Oleg Kiselyov showed how to take advantage of macros in runtime, if you're interested:

So don't interpret the code that you wrote as "a function that first defines syntax 'match-key' and then uses that new syntax to generate pattern matching code", because that's not how it works. The macro expander tries to substitute the usage of "match-key" macro with appropriate code immediately upon its usage, before you even run the function.

And although I'm not sure about this, because I don't know syntax-case macros well enough, I think that your code is a very good example of the difference between bound-identifier? and free-identifier?

(or isn't it?)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]