From: Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>
To: Maxime Devos <maximedevos@telenet.be>
Cc: Guile User <guile-user@gnu.org>
Subject: Re: Macro for replacing a placeholder in an expression
Date: Thu, 28 Jul 2022 08:39:45 +0000 [thread overview]
Message-ID: <61f3c2d2-78ec-cec6-5933-f0f8a5bc3aba@posteo.de> (raw)
In-Reply-To: <0c9ca13b-5cda-b575-4645-2040bb30ea53@telenet.be>
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)]
>>
>> [(_ 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?
>
> Greetings,
> Maxime
Thank you again for your help and explanations! Things are much clearer now!
Best regards,
Zelphir
--
repositories: https://notabug.org/ZelphirKaltstahl
next prev parent reply other threads:[~2022-07-28 8:39 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-27 23:57 Macro for replacing a placeholder in an expression Zelphir Kaltstahl
2022-07-28 0:55 ` Maxime Devos
2022-07-28 10:23 ` Zelphir Kaltstahl
2022-07-28 10:28 ` Maxime Devos
2022-07-30 15:42 ` Zelphir Kaltstahl
2022-07-30 20:44 ` Maxime Devos
2022-07-30 21:10 ` Maxime Devos
2022-07-30 21:13 ` Maxime Devos
2022-08-05 9:42 ` Linus Björnstam
2022-08-06 14:28 ` Zelphir Kaltstahl
2022-07-28 1:04 ` Maxime Devos
2022-07-28 8:39 ` Zelphir Kaltstahl [this message]
2022-07-28 9:48 ` Maxime Devos
2022-07-28 11:26 ` Zelphir Kaltstahl
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=61f3c2d2-78ec-cec6-5933-f0f8a5bc3aba@posteo.de \
--to=zelphirkaltstahl@posteo.de \
--cc=guile-user@gnu.org \
--cc=maximedevos@telenet.be \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).