guile-user
[Top][All Lists]
Advanced

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

Re: Macro for replacing a placeholder in an expression


From: Maxime Devos
Subject: Re: Macro for replacing a placeholder in an expression
Date: Thu, 28 Jul 2022 03:04:20 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0


On 28-07-2022 01:57, Zelphir Kaltstahl wrote:
scheme@(guile-user)> (define-syntax test
  (syntax-rules (lambda)
    [(_ (op args body* ...))
     ((test op) (test args) (test body* ...))]

    [(_ thing1 thing2 things* ...)
     ((test thing1) (test thing2 things* ...))]

    [(_ (thing))
     (thing)]

    [(_ thing)
     thing]))
scheme@(guile-user)> (test (lambda (a) (+ a 1)))
While compiling expression:
Syntax error:
unknown file:798:0: lambda: invalid argument list in subform ((a)) of (test (a))
~~~~

There seems to be something about a template like (one-thing) that I do not understand or something completely different is going on.

Here's what happening:

(test (lambda (a) (+ a 1))

--> because  the 'test' in the beginning is a macro

((test lambda) (test (a)) (test (+ a 1))

--> likewise

(lambda (test (a)) (test (+ a 1))

Now we end up with the 'lambda' macro. The lambda macro sees as argument list (test (a)) and interprets 'test' as the first argument, but the second part '(a)' is not an identifier so the lambda macro cannot do anything with that and tells you that by saying: lambda: invalid argument list in ....

This seems the same issue as in 'Re: boiler plate class generation, writing fresh variables with macros' to me but in a slightly different context

Syntax transformations in Scheme work from the outside to the inside, not the other way around, so you can't do things like this (define-class doesn't know what to do with this 'slot-machine' thing, it will reject it for not being a valid slot definition). However, you can define a syntax that generates the surrounding define-class and interprets things to insert the result of slot-matchine into a proper define-class form.
I consider a (in your case recursive, but in that case more like something like syntax-map (which can be defined recursively in terms of syntax-case)) syntax-case to be practical for this (more so than pure syntax-rules), see my other response.

Greetings,
Maxime

Attachment: OpenPGP_0x49E3EE22191725EE.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature
Description: OpenPGP digital signature


reply via email to

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