macro of macros
parent
8552b9f8da
commit
05c5b37316
@ -1,21 +1,44 @@
|
|||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require (for-syntax racket/base))
|
(require (for-syntax racket/base))
|
||||||
|
|
||||||
(provide macro-map)
|
(provide macro-map macro-for-each)
|
||||||
|
|
||||||
(define-syntax (macro-map stx)
|
|
||||||
(syntax-case stx ()
|
(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))
|
[(_ macro-name (list item0))
|
||||||
#'(cons
|
(joining-proc
|
||||||
(macro-name item0) '())]
|
(macro-name item0) ending-value)]
|
||||||
|
|
||||||
[(_ macro-name (list item0 items ...))
|
[(_ macro-name (list item0 items (... ...)))
|
||||||
#'(cons
|
(joining-proc
|
||||||
(macro-name item0)
|
(macro-name item0)
|
||||||
(macro-map macro-name (list items ...)))]))
|
(mappy-macro-name macro-name (list items (... ...))))])))
|
||||||
|
|
||||||
|
(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))
|
||||||
|
|
||||||
(define-syntax-rule (add x)
|
;(macro-for-each add '(24 25 30))
|
||||||
(+ 1 x))
|
|
||||||
|
|
||||||
(macro-map add (list 24 25 30))
|
|
Loading…
Reference in New Issue