macrosschemedefine-syntax

How to tranpose pattern sequence in Scheme macro?


I wrote the following Scheme macro.

(define-syntax join
  (syntax-rules ()
    ((join (var ...) (val ...) ...)
     '(((var val) ...)
       ...))))

When I try it

(join (a b c)
      (1 2 3)
      (2 4 6)
      (3 6 9))

it returns the following result.

(((a 1) (a 2) (a 3))
 ((b 2) (b 4) (b 6))
 ((c 3) (c 6) (c 9)))

But my intention was to write a macro, which returns the following result.

(((a 1) (b 2) (c 3))
 ((a 2) (b 4) (c 6))
 ((a 3) (b 6) (c 9)))

How the transpose the way the pattern matcher itemizes the var variable?


Update: I was told, that the join macro in this question is illegal according to R7RS 4.3.2:

Pattern variables that occur in subpatterns followed by one or more instances of the identifier ⟨ellipsis⟩ are allowed only in subtemplates that are followed by as many instances of ⟨ellipsis⟩.


Solution

  • Sorry but I got it myself. This seems to work.

    (define-syntax join
      (syntax-rules ()
        ((_ var val ...)
         (let-syntax ((j (syntax-rules ::: ()
                           ((_ (k :::) (v :::))
                            '((k v) :::)))))
           `(,(j var val) ...)))))