guile-user
[Top][All Lists]
Advanced

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

Re: Macro id-memv??, workings of tripple dot


From: Jean Abou Samra
Subject: Re: Macro id-memv??, workings of tripple dot
Date: Fri, 15 Mar 2024 09:03:48 +0100
User-agent: Evolution 3.50.4 (3.50.4-1.fc39)

> My question is: Do the ... in the case
> 
> ((test id _kt _kf) _kt) ...
> 
> produce one case for each identifier in the list?


Yes, they do.


> I am guessing that this is what they do. However, they are mentioned as 
> literals in the inner syntax-rules,


No, they aren't. The (id ...) form is expanded by the outer syntax-rules
form, as part of expanding the id-memv?? macro. They don't remain in the
expanded result. For example, if you call

(id-memv?? foo (foo bar baz) kt kf)

the expansion will look like

(let-syntax ((test
              (syntax-rules (foo bar baz)
                ((test foo _kt _kf) _kf)
                ((test bar _kt _kf) _kf)
                ((test baz _kt _kf) _kf)
                ((test otherwise _kt _kf) _kf))))
  (test foo kt kf))


> so I was thinking the expansion will simply put literally three dots there, 
> instead of understanding the three dots to mean "for each of the ids".
> 
> And also I still am unsure about whether the three dots work like this at all.


They do.


> When one puts the ... after a compound expression, that contains the thing, 
> that 
> the ... were after in the matching -- in this case they were after id, and id 
> is 
> contained in the compound expression (test id _kt _kf) _kt) -- does that make 
> the compound expression be generated for each thing matched?


Yes, see:

(syntax->datum
 (with-syntax ((simple #'a)
               ((compound ...) #'(b c d))
               (((nested-compound ...) ...) #'((e f g) (h i j))))
   #'(((simple compound nested-compound) ...) ...)))

⇒ (((a b e) (a c f) (a d g)) ((a b h) (a c i) (a d j)))


> But if this is the case, then I might be misunderstanding the Guile docs at 
> https://www.gnu.org/software/guile/manual/html_node/Syntax-Rules.html:
> 
> "Instances of a pattern variable in the template must be followed by an 
> ellipsis."

Note that this is talking about the patterns, not the syntax forms. But it
is slightly misleading: also in patterns it is perfectly possible to do
something like

(syntax->datum
 (with-syntax ((((a b) ...) #'((1 2) (3 4) (5 6))))
   #'((a ...) . (b ...))))

⇒ ((1 3 5) 2 4 6)

Note the pattern

((a b) ...)

An ellipsized pattern is recognized by the ellipsis, but it doesn't
need to follow a simple pattern variable, it can follow a nested
pattern.

Best,
Jean

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

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