1.2.4 Importing Scheme in LilyPond

The above example shows how to ‘export’ music expressions from the input to the Scheme interpreter. The opposite is also possible. By placing it after $, a Scheme value is interpreted as if it were entered in LilyPond syntax. Instead of defining \twice, the example above could also have been written as

…
$(make-sequential-music newLa)

You can use $ with a Scheme expression anywhere you could use \name after having assigned the Scheme expression to a variable name. This replacement happens in the ‘lexer’, so LilyPond is not even aware of the difference.

One drawback, however, is that of timing. If we had been using $ instead of # for defining newLa in the above example, the following Scheme definition would have failed because traLaLa would not yet have been defined. For an explanation of this timing problem, LilyPond Scheme syntax.

A further convenience can be the ‘list splicing’ operators $@ and #@ for inserting the elements of a list in the surrounding context. Using those, the last part of the example could have been written as

…
{ #@newLa }

Here, every element of the list stored in newLa is taken in sequence and inserted into the list, as if we had written

{ #(first newLa) #(second newLa) }

Now in all of these forms, the Scheme code is evaluated while the input is still being consumed, either in the lexer or in the parser. If you need it to be executed at a later point of time, check out Void scheme functions, or store it in a procedure:

#(define (nopc)
  (ly:set-option 'point-and-click #f))

…
#(nopc)
{ c'4 }

Known issues and warnings

Mixing Scheme and LilyPond variables is not possible with the ‘--safe’ option.


Andere talen: deutsch, español, français.
About automatic language selection.

LilyPond — Extending v2.21.0 (ontwikkelingstak).