Reputation: 65
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.
Upvotes: 2
Views: 255
Reputation: 70235
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.
Upvotes: 7