Discussion:
Use String to reference Variable
Pedro Pessoa
2018-12-04 03:28:41 UTC
Permalink
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?




--
Sent from: http://lilypond.1069038.n5.nabble.com/User-f3.html
Jan-Peter Voigt
2018-12-04 08:37:27 UTC
Permalink
Hello Pedro,
Post by Pedro Pessoa
Hello!
I want a function that takes a string as arg an from that produces a valid
%%% 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
%%%
---
%%%
\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")))
#})


Jan-Peter
David Kastrup
2018-12-04 09:33:11 UTC
Permalink
Post by Jan-Peter Voigt
Hello Pedro,
Post by Pedro Pessoa
Hello!
I want a function that takes a string as arg an from that produces a valid
%%% 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
%%%
---
%%%
\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 '#').
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
Loading...