如何在方案中的列表中映射宏?

jos*_*osh 5 macros scheme

我有一个计划宏和一个长列表,我想在列表中映射宏,就像它是一个函数一样。我怎样才能使用 R5RS 做到这一点?

该宏接受几个参数:

(mac a b c d)
Run Code Online (Sandbox Code Playgroud)

名单有

(define my-list ((a1 b1 c1 d1)
                 (a2 b2 c2 d2)
                 ...
                 (an bn cn dn)))
Run Code Online (Sandbox Code Playgroud)

我想要这个:

(begin
   (mac a1 b1 c1 d2)
   (mac a2 b2 c2 d2)
   ...
   (mac an bn cn dn))
Run Code Online (Sandbox Code Playgroud)

(顺便说一句,正如你所看到的,我也想拼接参数列表)

Dav*_*rak 2

扩展 z5h 使用 eval 的答案,以下方法显示了如果交互环境在使用的 R5RS 版本中实现,则如何编写地图宏宏:

(define test-list '((1 2 3 4)
                    (5 6 7 8)))

;Or if your version of scheme implments interaction-environment then:
(define-syntax trade
  (syntax-rules ()
    ((_ a b c d) (display (list b a d c)))))

;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;Careful this is not really mapping. More like combined map and apply.
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
(define-syntax map-macro
  (syntax-rules ()
    ((_ mac ls) (let ((mac-list (map (lambda (lst) (cons 'trade lst)) ls)))
                          (eval 
                           `(begin
                              ,@mac-list)
                           (interaction-environment))))
                        ))

(map-macro trade test-list)
;outputs: (2 1 4 3)(6 5 8 7)
Run Code Online (Sandbox Code Playgroud)

因此,最后一个地图宏调用将评估以下内容:

最终从(地图宏观贸易测试列表)得到的评估是:

(begin
  (trade 1 2 3 4)
  (trade 5 6 7 8))
Run Code Online (Sandbox Code Playgroud)

这不完全是一张地图,但我相信它确实回答了你的问题。