user6085089
user6085089

Reputation:

In Racket, how do I create a syntax rule which can deal with multiple of one parameter?

Kind of difficult to word the question in the title.

(define-syntax func
  (syntax-rules ()
    ((func a b c (d e) ...) (cond ((and (not (empty? d)) (not (empty? e))) (+ d e))
                                  )
                            )
    )
  )

If someone calls (func a b c (1 1) (2 2)), I would like it to add all d's and e's together. First, my code above produces an error

syntax: missing ellipsis with pattern variable in template in: d

and if it didn't even give me that error, I'm not even sure if it would add all of them together. I would also like it to do other things in case d and e were not provided, so I put it in a cond.

Thank you.

Edit:

(define-syntax func
  (syntax-rules ()
    ((func a b c (d e) ...)
     (cond
       ((and
         (not (empty? d))
         (not (empty? e)))
        (+ d e))))))

Upvotes: 0

Views: 563

Answers (1)

Sylwester
Sylwester

Reputation: 48745

The pattern something ... will match zero or more elements. Thus in you pattern (func a b c) will match the rule.

If a pattern has elipses in the pattern it needs elipses in the expansion. Eg.

(define-syntax test
  (syntax-rules ()
    ((_ a b ...)
     (if a (begin #t b ...) #f))))

(test 1)    ; ==> #t
(test 1 2)  ; ==> 2
(test #f 2) ; ==> #f

Upvotes: 0

Related Questions