lilypond-user
[Top][All Lists]
Advanced

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

Re: String concatenation in sheme?


From: Nicolas Sceaux
Subject: Re: String concatenation in sheme?
Date: Wed, 15 Nov 2006 22:59:10 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (darwin)

"Rick Hansen (aka RickH)" <address@hidden> writes:

> I'm not a Scheme programmer, but how does one concatenate more data onto the
> end of an existing variable?  Is there a string concatenation expression in
> scheme that will let someone "add onto" the end of a string using subsequent
> commands.  For example (below is in a java pseudocode where "+=" means
> concatenate to self) how to do same in scheme?
>
> varVoiceMelody = \relative c' { a4 b c
> varVoiceMelody += d | e f
> varVoiceMelody += g a |
> varVoiceMelody += }
>
> So that the final value of varVoiceMelody is left being:
>
> \relative c' { a4 b c d | e f g a | }
>
> I want to write some macros that when stated simply append their output to
> variables already existing in my lp templates, these macros hopefully will
> simply generate "varVoiceMelody += whatever" expressions to append to the
> proper master variables in the lp code.


What you want is not string concatenation. { a4 b c d } is a music
expression, not a string, so look how it is stored internally:

  \displayMusic { a b c d | }
  \displayMusic { e f g a | }
  \displayMusic { a b c d | e f g a | }

You'll see a SequenceMusic element, containing a list of EventChord and
BarChack elements in its 'elements property. So what you'd like is
appending two lists. But there is another, easier, solution. Compare:

  \displayMusic { a b c d | }
  \displayMusic { e f g a | }
  \displayMusic { { a b c d | } { e f g a | } }

(the latter being equivalent to { a4 b c d | e f g a | })

There, you build a SequentialMusic object which elements are the
previous value of the variable, and the music that is to be appended. A
possible implementation (not tested):

appendToVariable =
#(define-music-function (parser location symbol music)
                        (symbol? ly:music?)
   (ly:parser-define! parser symbol
     (make-music 'SequentialMusic 
       'elements (list (ly:parser-lookup parser symbol)
                       music)))
   (make-music 'SequentialMusic 'void #t))

myvar = { a4 b c d | }
\appendToVariable #'myvar { e f g a | }

{ \myvar }

nicolas




reply via email to

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