lilypond-user
[Top][All Lists]
Advanced

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

Scratching my head around define-macro and variable evaluation


From: Stefano Troncaro
Subject: Scratching my head around define-macro and variable evaluation
Date: Wed, 28 Mar 2018 16:05:02 -0300

Hi everyone!

I have a question about the following example:
\version "2.19.80"

#(define-macro (why-the-difference obj)
   (display (format "~a , " obj))
   `(display (format "~a\n" ,obj)))

#(why-the-difference (list 1 2 3))
% => (list 1 2 3) , (1 2 3)

#(define var (list 1 2 3))

#(why-the-difference var)
% => var , (1 2 3)
So, I assume that the difference is because the macro has access to what is typed, and uses that to produce an _expression_ that is later evaluated. So, in the first example, (list 1 2 3) was typed, so that's shown in the output before the comma. While in the second example, var was typed, so the symbol var is shown instead.

Is there a way to evaluate the symbol inside the body of the macro?

I tried the following to no avail:
\version "2.19.80"

#(use-modules (ice-9 r5rs))

#(define-macro (my-attempt obj)
   (display (format "~a , " (eval 'obj (interaction-environment))))
   `(display (format "~a\n" ,obj)))

#(define var (list 1 2 3))

#(my-attempt var)
This generates the error 'unbound variable: obj'. I don't understand why obj is not considered defined, when if I use obj I get the symbol var (as the first snippet showed). Anyways, I *can* evaluate var:
\version "2.19.80"

#(use-modules (ice-9 r5rs))

#(define-macro (my-attempt obj)
   (display (format "~a , " (eval 'var (interaction-environment))))
   `(display (format "~a\n" ,obj)))

#(define var (list 1 2 3))

#(my-attempt var)
% => (1 2 3) , (1 2 3)

Which makes sense. This achieves what I want but it is not useful because I need to know the name of the variable before-hand, so it will not work dynamically.

Does anyone know of a way around this?

Thank you in advance!
Stéfano

reply via email to

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