[Top][All Lists]

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

Re: Macro for replacing a placeholder in an expression

From: Zelphir Kaltstahl
Subject: Re: Macro for replacing a placeholder in an expression
Date: Thu, 28 Jul 2022 08:39:45 +0000

Hello Maxime!

Thank you for your quick response! (Mailing list saves me again! Yay!)

On 7/28/22 03:04, Maxime Devos wrote:

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)
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 ....

Ahhh now I get it! lambda is also a macro … I did not think of that.

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.

And this explains the order of expansion! Something I've already been wondering about, what the order is or what the rules are.

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.

I aimed to do everything with syntax-rules, as the simplest means, but when writing the code I have, I hit the snag, that one could not have multiple ellipses at the same level of nesting in the patterns. After some thinking I found the solution to build up a temporary list, which then is of course 1 deeper level of nesting, where I could then use ellipses again. I felt quite clever doing that trick. Maybe I could implement a syntax-map using that trick and then use syntax-map in my macro instead.

I have a question regarding syntax-case:

If I use it, does my code become less portable to other Schemes?

And regarding syntax-rules:

How portable are macros, which exclusively use syntax-rules?


Thank you again for your help and explanations! Things are much clearer now!

Best regards,


reply via email to

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