lilypond-user
[Top][All Lists]
Advanced

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

Re: Use String to reference Variable


From: David Kastrup
Subject: Re: Use String to reference Variable
Date: Tue, 04 Dec 2018 10:33:11 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Jan-Peter Voigt <address@hidden> writes:

> Hello Pedro,
>
> Am 04.12.18 um 04:28 schrieb Pedro Pessoa:
>> Hello!
>> I want a function that takes a string as arg an from that produces a valid
>> variable reference, as follows:
>> 
>> %%% pseudo 
>> Nabc={a1 d e f}
>> Nxyz={b1 e a d}
>> 
>> fun=
>> #(define-music-function (x)(string?)
>>    #{
>>      <<
>>        #(concat x "abc")
>>        \\
>>        #(concat x "xyz")
>>      >>
>>    #})
>> 
>> \fun "N" %produces parallel music with Nabc and Nxyz
>> %%%
>> 
>> ---
>> 
>> I've ran this test:
>> 
>> %%%
>> \Nabc={some music}
>> (display (string->symbol (string-append "N" "abc")))
>> %%%
>> 
>> It outputs "Nabc", not the music content of Nabc, as I expected.
>> Why is that? How do I make it point to the actual music?
> The string is converted to a symbol and a symbol is a primitive datatype
> in guile-scheme. To receive the value of the variable you have to ask
> the parser. To place the result in the music you should use an instant
> scheme expression (introduced by '$' not '#').
>
> HTH:
>
> fun=
> #(define-music-function (x)(string?)
>    #{
>      <<
>        $(ly:parser-lookup (string->symbol (string-append x "abc")))
>        \\
>        $(ly:parser-lookup (string->symbol (string-append x "xyz")))
>      >>
>    #})

In this particular case, # would have worked though $ tends to work in
more cases.  However, $ also creates a _copy_ of the music while #
doesn't.  If the music ends up in a \relative or \transpose or other
construct modifying its content in place, having a copy is important so
that the original variable does not get changed.

\xabc creates a copy like $xabc does, while #xabc doesn't.

-- 
David Kastrup



reply via email to

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