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

发布于 2024-08-28 21:19:07 字数 426 浏览 3 评论 0原文

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

该宏接受多个参数:

(mac a b c d)

列表有

(define my-list ((a1 b1 c1 d1)
                 (a2 b2 c2 d2)
                 ...
                 (an bn cn dn)))

我想要这个:(

(begin
   (mac a1 b1 c1 d2)
   (mac a2 b2 c2 d2)
   ...
   (mac an bn cn dn))

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

I have a Scheme macro and a long list, and I'd like to map the macro across the list, just as if it were a function. How can I do that using R5RS?

The macro accepts several arguments:

(mac a b c d)

The list has

(define my-list ((a1 b1 c1 d1)
                 (a2 b2 c2 d2)
                 ...
                 (an bn cn dn)))

And I'd like to have this:

(begin
   (mac a1 b1 c1 d2)
   (mac a2 b2 c2 d2)
   ...
   (mac an bn cn dn))

(By the way, as you can see I'd like to splice the list of arguments too)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彼岸花ソ最美的依靠 2024-09-04 21:19:07

扩展 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)

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

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

(begin
  (trade 1 2 3 4)
  (trade 5 6 7 8))

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

Expanding on z5h's answer of using eval, the methods below show how a map-macro macro can be written if interaction-environment in implemented in the version of R5RS in use:

(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)

So that last map-macro call evaluates the following:

What ends up getting evaluated from (map-macro trade test-list) is:

(begin
  (trade 1 2 3 4)
  (trade 5 6 7 8))

Which is not quite a map, but I believe it does answers your question.

音盲 2024-09-04 21:19:07

语法扩展被扩展为
评估开始时的核心表格
(编译或解释之前)
通过语法扩展器。 ——戴布维格,“
方案编程语言:

宏根据语法进行操作。这发生在编译或执行之前。它为您提供了编写相同代码的另一种方式。

函数对变量和值(可能是列表、原子、数字等)进行操作,调用函数时变量和值的值是已知的。

所以映射宏没有意义。您要求调用很久以前已经发生的事情(宏扩展)。

如果您需要一些东西来为您编写代码并在运行时对其进行评估,那么可能是您需要eval的情况之一。

Syntactic extensions are expanded into
core forms at the start of evaluation
(before compilation or interpretation)
by a syntax expander. -Dybvig, "The
Scheme Programming Language:

A macro operates on syntax. This happens before compilation or execution. It gives you another way of writing the same code.

A function operates on variables and values (which might be lists, atoms, numbers, etc) who's value is known when the function is invoked.

So mapping a macro doesn't make sense. You're asking to invoke something (macro expansion) that already happened long ago.

If you need something to write code for you and evaluate it at runtime, then might be one of those cases where you need eval.

原来分手还会想你 2024-09-04 21:19:07

想要

(map (lambda (l) (mac (car l) (caar l) (caaar l) (caaaar l))) mylist)

工作之类的吗?

Would something like

(map (lambda (l) (mac (car l) (caar l) (caaar l) (caaaar l))) mylist)

work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文