2015-03-06 15:51 GMT+01:00 Federico Beffa <be...@ieee.org>:

> Hi,
>
> 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 ...)
>      value)
>     (((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 ...)
     value)
    (((key value) rest ...)
     (key->value (cdr meta)))
    (_
     'key-not-found)))

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:
http://okmij.org/ftp/Scheme/macros.html#first-class-macros).

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