How to match a macro in a list on a chart?

I have a Scheme macro and a long list, and I would like to display the macro in the list as if it were a function. How can I do this with R5RS?

A macro takes 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 would 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 would also like to combine a list of arguments)

+3
source share
3 answers

Turning around z5h's answer to using eval, the following methods show how macro macros can be written if the interaction environment is implemented in the version of R5RS used:

(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 the last call to map-macro evaluates the following:

The result is an estimate (map-macro trade test-list):

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

, , .

+1

( ) . -, " :

. . .

( , , ..), .

. - (), .

- , , eval.

+2

There will be something like

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

Job?

0
source

Source: https://habr.com/ru/post/1741145/


All Articles