[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Variables as variables
From: |
David Kastrup |
Subject: |
Re: Variables as variables |
Date: |
Mon, 20 Jan 2014 16:23:12 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) |
Johan Vromans <address@hidden> writes:
> In LilyPond terms, in
>
> namedMusic = { ... }
>
> 'namedMusic' is called a variable, identifier or macro.
>
> Normal LilyPond behaviour is
>
> aaa = { c4 d e f }
> bbb = { a4 a a a \aaa b4 b b b }
> \score { \bbb }
>
> This produces { a4 a a a c4 d e f b4 b b b } .
>
> Now I want to re-use bbb with a different value of aaa. Like a real
> variable as known from other programming languages.
>
> aaa = { c4 d e f }
> bbb = { a4 a a a \aaa b4 b b b }
> \score { \bbb } % a4 a a a c4 d e f b4 b b b
> aaa = { f4 e d c }
> \score { \bbb } % a4 a a a f4 e d c b4 b b b
That's not "like a real variable as known from other programming
language". If I write
aaa = 3
bbb = aaa + 1
print ( bbb )
aaa = 5
print ( bbb )
I know of no programming language which will output 6 the second time
round. You are not thinking of a variable, but of a parametric
procedure.
> This works, but it's not elegant.
>
> bbb =
> #(define-music-function
> (parser location)
> ()
> #{
> a4 a a a \aaa b4 b b b
> #})
That's delayed execution. In my opinion, it makes more sense as a
parametric procedure:
bbb =
#(define-music-function
(parser location aaa)
(ly:music?)
#{
a4 a a a #aaa b4 b b b
#})
You can call it as \bbb \aaa or \bbb { f4 e d c } then.
> Do you have better (simpler, more elegant) suggestions?
I don't think that there are "more elegant" solutions available here.
While the given solutions might conceivably gain a bit more syntactic
sugar at one point of time, what actually _happens_ is unlikely to fall
in any different categories than what the current solutions do.
--
David Kastrup