1
votes

I've been looking over the syntax for defining a syntax rule however I am still stuck.

I want to define a rule with this signature: (define-syntax-rule (for {val in lst} return res).

Basically it simply applies map given a val and a lst. But I am struggling with the syntax. This is what I've come up with so far.. .... (syntax-rules () ((for val in lst) (map (val lst)))) ...

The input would be something like:

(for {val in '(0 1 2 3 4)} return (- val 1) )

and output (-1 0 1 2 3), as if map were called on the list.

1
You can't use define-syntax-rule since you have literals in the definition (ie the in and return). @GoZoner's answer (if you add parens around the (val in list)) gives you what you want. (And in Racket, curly braces are the same as regular parens.) - stchang
By the way, Racket comes with for/list which does the same thing as what you are trying to define. (for/list ([val '(0 1 2 3 4)]) (- val 1)) => '(-1 0 1 2 3) - stchang

1 Answers

6
votes

This gets it done:

(define-syntax for
  (syntax-rules (in return)
    ((for val in list return exp)
     (map (lambda (val) exp) list))))

> (for x in '(0 1 2 3) return (- x 1))
(-1 0 1 2)

Essentially this is exactly as you've described. You need in and return declared as literals.