guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to use-modules within macro?


From: pelzflorian (Florian Pelz)
Subject: Re: How to use-modules within macro?
Date: Wed, 4 Sep 2019 13:24:38 +0200
User-agent: NeoMutt/20180716

On Thu, Aug 29, 2019 at 07:04:07PM -0400, Mark H Weaver wrote:
> Hi Florian,
> 
> "pelzflorian (Florian Pelz)" <address@hidden> writes:
> 
> > I am writing a Guile macro to manipulate Scheme code and am stuck on
> > what I hope is a simple problem and it would be nice if you could
> > explain.  I try:
> >
> > (define-syntax O
> >   (lambda (x)
> >     (syntax-case x ()
> >       ((_)
> >        #`(begin (use-modules (ice-9 local-eval))
> >                 (local-eval 42 (the-environment)))))))
> > (pk (O))
> 
> This approach is misguided and unnecessary.  You don't need to include
> 'use-modules' in your macro expansion, and it's best avoided.
> 
> […]

Your explanation helped a lot.

To retain unhygienic references, I am now using datum->syntax instead
of local-eval.  It is much better.  For example, to make a macro that
increments all numbers in a given program by one:

(use-modules (ice-9 match))
(define-syntax one-more
  (lambda (x)
    (syntax-case x ()
      ((_ exp)
       (datum->syntax
        #'exp
        (let loop ((y (syntax->datum #'exp)))
          (match y
            ((? number?) (1+ y))
            ((? list?) (map loop y))
            (else y))))))))

(let ((four 4))
  (one-more (* 2 3 four)))

Yields 48.  I hope this is the right approach for rewriting programs.

Regards,
Florian



reply via email to

[Prev in Thread] Current Thread [Next in Thread]