lilypond-user
[Top][All Lists]
Advanced

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

Re: Scratching my head around define-macro and variable evaluation


From: David Kastrup
Subject: Re: Scratching my head around define-macro and variable evaluation
Date: Wed, 28 Mar 2018 21:23:57 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux)

Stefano Troncaro <address@hidden> writes:

> 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?

Not reliably.  It's a rather loosely defined point of time.

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

Well, that's completely wrong.  (interaction-environment) is a _global_
environment.  It does not have access to local variables/symbols like
obj.  So you'd want to write

(eval obj (interaction-environment)) if at all, and even then this will
only work for global variables.

>> #(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?

Apparently you don't understand what a macro does.  A macro receives its
arguments _unevaluated_, and the result is later evaluated in the
closure where the macro is called.  (eval obj (interaction-environment))
is exactly equivalent to (eval 'var (interaction-environment)) here.

-- 
David Kastrup



reply via email to

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