Why does the same macro work with either 2 or 3 pattern variables? 
Author Message
 Why does the same macro work with either 2 or 3 pattern variables?

In documentation of structured macros, I've seen two definitions of the "or"
construct used as examples:

from "An Introduction to Scheme and its Implementation"

(define-syntax or
   (syntax-rules ()
      ((or)             ; OR of zero arguments
       #f)              ;   is always false
      ((or a)           ; OR of one argument
       a)               ;   is equivalent to the argument expression
      ((or a b c ...)   ; OR of two or more arguments
       (let ((temp a))  ;   is the first or the OR of the rest
          (if temp          
              temp            
              (or b c ...))))))

and from "Revised(5) Report on the Algorithmic Language Scheme":

  (define-syntax or
    (syntax-rules ()
      ((or) #f)
      ((or test) test)
      ((or test1 test2 ...)
       (let ((x test1))
         (if x x (or test2 ...))))))

Note that the pattern  "(or a b c ...)" in the first definition corresponds to
the pattern "(or test1 test2 ...)" in the second.  Both definitions work (I
tried them).

Why would a list with three elements (counting the "or") match "(or a b c
...)"?  

I though perhap that "c" matched an "empty" but when I tried testing that I got
an error - it has no value.

Then I thought that with the elipses, perhaps it didn't matter how many unused
pattern variables there were, so I tried replacing "(or a b c ...)" with "(or a
b c d ...)", but then it wouldn't match a three item list at all!

Does this mean that if I want a macro with a variable number, but at least "n"
parameters then I have to make a pattern with "n+1" pattern variables?

If I wanted to have a special case matching a three item list, could I put an
"(or a b)" pattern before "(or a b c ...)"?

Joshua Scholar



Wed, 03 Jul 2002 03:00:00 GMT  
 Why does the same macro work with either 2 or 3 pattern variables?
Neonfish99:

Quote:
> Does this mean that if I want a macro with a variable number,
> but at least "n"
> parameters then I have to make a pattern with "n+1" pattern variables?

Cf. R5RS 4.3.2, p. 15:
    A subpattern followed by ... can match ZERO OR MORE elements of
    the input.  (,,,)  Within a pattern the identifier ... must follow
    the last element of a nonempty sequence of subpatterns.

So yes, you want n+1 pattern variables followed by the ellipsis.

Quote:
> If I wanted to have a special case matching a three item list,
> could I put an
> "(or a b)" pattern before "(or a b c ...)"?

Counting the OR, yes.

rthappe



Fri, 05 Jul 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. mine doesn′t work either...

2. Why don't my global variables work?

3. Value of f90 module variables not visible from either gdb or idb

4. MS thesis on Macros Done Right :-) available

5. Re-writing abstractions, or Lambda: the ultimate pattern macro

6. Matching weird patterns with macros

7. A simple pattern matching macro

8. A pattern matching macro in scheme

9. eval-time macro-pattern matching

10. Re-writing abstractions, or Lambda: the ultimate pattern macro

11. Appending Patterns in R5RS Macros

12. A simple pattern matching macro

 

 
Powered by phpBB® Forum Software