开发者

How do I map a macro across a list in Scheme?

开发者 https://www.devze.com 2022-12-27 10:47 出处:网络
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?

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)


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.


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.


Would something like

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

work?

0

精彩评论

暂无评论...
验证码 换一张
取 消