0
votes

I need to be able to match patterns like this:

(lambda (<param> ...) <stmt> ...)

using match-lambda and return something like this:

(CLOSURE ENV '(<param> ...) (λ (ENV) <stmt> ...)

where the list of parameters is quoted.

The closest I can get is:

(`(lambda (,<param> . ,<params>) . ,<stmts>) 
 `(CLOSURE ENV (,<param> . <params>) (lambda (ENV) (,@<stmts>))))

For instance, '(lambda (ls1 ls2) (append ls1 ls2))) returns:

'(CLOSURE
  ENV
  (ls1 ls2)
  (lambda (ENV) ((append ls1 ls2))))

but I can't get the parameter list to be quoted.

In answers to previous posts I have been told that the "." in the pattern must precede the last element in a list. However I have found that this isn't correct. For instance:

(define function-call
  (match-rewriter
   (`(,<func> . ,<args>)
    `(CALL ,<func> (list ,@<args>)))) (+ 1 2) )

returns:

'(CALL + (list 1 2))

where args has clearly match both the 1 and the 2.

If anyone can give me some advice on how to fix this pattern it would be greatly appreciated.

Thanks.

1
Hiya; regarding your comment about (my comment about) the placement of the dot: the point is that the dot must precede at most one element of the pattern, as it does here. - John Clements
Oh, I see. Thanks for following up with me. - Schemer

1 Answers

1
votes

You need to put the ' literally into the output. This can be tricky when using quasiquote, so I'll avoid that in my answer. Here's the pattern and right-hand side you want:

[`(lambda (,<param> . ,<params>) . ,<stmts>)
 (list 'CLOSURE 'ENV (list 'quote (cons <param> <params>)) 
       (list 'lambda '(ENV) <stmts>))]