From 05c5b373163fa173e8884fbc38249b097ff8ab5c Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 28 Feb 2014 12:09:25 -0800 Subject: [PATCH] macro of macros --- macro.rkt | 51 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/macro.rkt b/macro.rkt index 13abffb..28c5d06 100644 --- a/macro.rkt +++ b/macro.rkt @@ -1,21 +1,44 @@ #lang racket/base (require (for-syntax racket/base)) -(provide macro-map) +(provide macro-map macro-for-each) -(define-syntax (macro-map stx) - (syntax-case stx () - [(_ macro-name (list item0)) - #'(cons - (macro-name item0) '())] - - [(_ macro-name (list item0 items ...)) - #'(cons - (macro-name item0) - (macro-map macro-name (list items ...)))])) +(define-syntax-rule (make-mappy-macro mappy-macro-name joining-proc ending-value) + (define-syntax mappy-macro-name + (syntax-id-rules () + ;; convert quote form into list form + [(_ macro-name (quote (items (... ...)))) + (mappy-macro-name macro-name (list items (... ...)))] + + ;; catch this case first, because it would also match the next one + [(_ macro-name (list item0)) + (joining-proc + (macro-name item0) ending-value)] + + [(_ macro-name (list item0 items (... ...))) + (joining-proc + (macro-name item0) + (mappy-macro-name macro-name (list items (... ...))))]))) -(define-syntax-rule (add x) - (+ 1 x)) +(make-mappy-macro macro-for-each begin (void)) +(make-mappy-macro macro-map cons '()) + + + +(define-syntax-rule (add pair) + (+ (car pair) (cdr pair))) + + +;; this matches first case - why? +(macro-for-each add (list (cons 12 20))) + + +;(macro-map add (list 24 25 30)) +;(macro-map add '(24 25 30)) + + +;(macro-map-old add (list 24 25 30)) + +;(macro-for-each add '(24 25 30)) -(macro-map add (list 24 25 30)) \ No newline at end of file