0

I am trying to pattern match calls to letrec using match-lambda. It seems to me that this pattern:

(match-lambda
  (`(letrec ((,<var> ,<val>) . (,<vars> ,<vals>)) ,<expr> . ,<exprs>)
   `(<match>))

should match calls of the form:

(letrec ((<var> <val>) ...) <expr> ...)

But, of course, this isn't working.

Any advice is appreciated.

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
Schemer
  • 1,635
  • 4
  • 19
  • 39

2 Answers2

1

I think you need to use the ... syntax in match-lambda:

(match-lambda
  (`(letrec ((,<var> ,<val>) ...) ,<expr> ...)
    body))
Jeremiah Willcock
  • 30,161
  • 7
  • 76
  • 78
  • Why? `(x y) and (list `x `y) are identical, aren't they? – configurator Mar 18 '11 at 13:30
  • @configurator: I don't think they are equivalent as `match` patterns, at least according to my reading of the documentation. – Jeremiah Willcock Mar 18 '11 at 18:00
  • They are equivalent objects. The match function accepts the objects only after `read` has already converted them into lists with quoted and unquoted stuff. – configurator Mar 18 '11 at 21:36
  • @configurator: The reader macros for quasiquote and unquote just produce `(quasiquote ...)` and `(unquote ...)`; the conversion to `list` and such is done by normal macros which wouldn't be called in `match` patterns. – Jeremiah Willcock Mar 19 '11 at 00:41
  • @configurator: It appears here that `...` does work in quasipatterns, though, which I do not believe is documented. I'll edit my answer. – Jeremiah Willcock Mar 19 '11 at 00:45
0

I'm not sure, but I think the problem might be with the ((,<var> ,<val>) . (,<vars> ,<vals>)). This is identical to ((,<var> ,<val>) ,<vars> ,<vals>) which isn't what you want. Perhaps try something like ((,<var> ,<val>) . ,<vars-vals>))?


I also looked into the documentation and it seems like letrec shouldn't be part of your expression, and you should possibly use match-lambda*.

Try

(match-lambda
  (`(((,<var> ,<val>) . ,<vars-vals>) ,<expr> . ,<exprs>)
   `(<match>)))

(although of course, I could be wrong)

configurator
  • 40,828
  • 14
  • 81
  • 115