lilypond-user
[Top][All Lists]
Advanced

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

Re: Scoped variables


From: Aaron Hill
Subject: Re: Scoped variables
Date: Thu, 07 Nov 2019 08:38:37 -0800
User-agent: Roundcube Webmail/1.3.8

On 2019-11-07 7:33 am, Peter Toye wrote:
This raises a further question which I can't see answered in the
documentation: exactly what characters are allowed in a Lilypond
variable name? NR says that the convention is alphabetic characters
only, but you seem to have found that digits and full-stops are
allowed. Is this documented, or did you experiment?

Only the part up to the first period or comma is the name of the variable. Each part after is defining the keys for a (possibly nested) association list:

%%%%
Variable . Key , 1 = "foo"
#(format #t "\n~a" Variable)
#(format #t "\n~a" #{ \Variable.Key.1 #})
%%%%
====
Parsing...
((Key (1 . foo)))
foo
Success: compilation successfully completed
====

This creates a top-level variable named 'Variable' that has the equivalent Scheme definition:

%%%%
#(define Variable '((Key . ((1 . "foo")))))
#(format #t "\n~a" Variable)
#(format #t "\n~a" #{ \Variable.Key.1 #})
%%%%
====
Parsing...
((Key (1 . foo)))
foo
Success: compilation successfully completed
====

It seems that numbers are permitted for naming of the association list keys, but the top-level variable name must be alphabetic only unless it is quoted:

%%%%
"12?!" = "blah"
#(format #t "\n~a" #{ \"12?!" #})
#(format #t "\n~a" (eval (string->symbol "12?!") (interaction-environment)))
%%%%
====
Parsing...
blah
blah
Success: compilation successfully completed
====

An interesting detail with using numbers is that the resulting key is numeric rather than a symbol. To get a symbol, you would need to use quotes:

%%%%
Thing, 1 = "one"
Thing, "2" = "two"
#(format #t "\n~a" Thing)
#(format #t "\n~a" (map (lambda (x) (symbol? (car x))) Thing))
%%%%
====
Parsing...
((#{2}# . two) (1 . one))
(#t #f)
Success: compilation successfully completed
====

Mind you, I am uncertain why you want #{2}# over just 2.

Finally, LilyPond only prepends new association list entries rather than overwriting any existing ones:

%%%%
Thing, Key = 12
Thing, Key = -34
#(format #t "\n~a" Thing)
#(format #t "\n~a" #{ \Thing.Key #})
%%%%
====
Parsing...
((Key . -34) (Key . 12))
-34
Success: compilation successfully completed
====

But since lookup returns the first matching key, it will appear as if it is overwriting.


-- Aaron Hill



reply via email to

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