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 ()
|
|
||||||
[(_ 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)
|
(make-mappy-macro macro-for-each begin (void))
|
||||||
(+ 1 x))
|
(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))
|
|
Loading…
Reference in New Issue