[racket] Macro Help

From: Helmut Rohrbacher (helmut.rohrbacher at gmail.com)
Date: Sun Feb 26 19:18:37 EST 2012

I'm trying to write a macro provide a new way to define functions so that:


(define fold-left
  (function
   _ i '() -> i
   f i (cons x xs) -> (fold-left f (f i x) xs)))


Would be valid syntax that would expand to:

(define fold-left
  (match-lambda*
    [(list _ i '()) i]
    [(list f i (cons x xs)) (fold-left f (f i x) xs)]))

So far I have been able to get there half-way by wrapping each *args ...* ->
*body* pattern in parens, which makes the above declaration look like this:

(define fold-left
  (function
   [_ i '() -> i]
   [f i (cons x xs) -> (fold-left f (f i x) xs)]))

By using this macro:

(define-syntax function
  (syntax-rules (->)
    [(_ (args ... -> body) ...)
     (match-lambda*
       [(list args ...) body]
       ...)]))


What macro voodoo do I need to harness in order to be able to implement the
first definition syntax where the *args ... -> body* segments need not be
wrapped in parens?

BTW, first time poster, so go easy on me :)

Thanks a bunch!
-- Helmut
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120226/28bb39d6/attachment.html>

Posted on the users mailing list.