]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/extending/scheme-tutorial.itely
Doc -- add structure to Scheme tutorial
[lilypond.git] / Documentation / extending / scheme-tutorial.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2
3 @ignore
4     Translation of GIT committish: FILL-IN-HEAD-COMMITTISH
5
6     When revising a translation, copy the HEAD committish of the
7     version that you are working on.  See TRANSLATION for details.
8 @end ignore
9
10 @c \version "2.12.0"
11
12 @node Scheme tutorial
13 @chapter Scheme tutorial
14
15 @funindex #
16 @cindex Scheme
17 @cindex GUILE
18 @cindex Scheme, in-line code
19 @cindex accessing Scheme
20 @cindex evaluating Scheme
21 @cindex LISP
22
23 LilyPond uses the Scheme programming language, both as part of the
24 input syntax, and as internal mechanism to glue modules of the program
25 together.  This section is a very brief overview of entering data in
26 Scheme.  If you want to know more about Scheme, see
27 @uref{http://@/www@/.schemers@/.org}.
28
29 LilyPond uses the GNU Guile implementation of Scheme, which is
30 based on the Scheme @qq{R5RS} standard. If you are learning Scheme
31 to use with LilyPond, working with a different implementation (or
32 referring to a different standard) is not recommended. Information
33 on guile can be found at @uref{http://www.gnu.org/software/guile/}.
34 The @qq{R5RS} Scheme standard is located at
35 @uref{http://www.schemers.org/Documents/Standards/R5RS/}.
36
37 The LilyPond installation also includes the Guile implementation of
38 Scheme.  On most systems you can experiment in a Scheme sandbox by
39 opening a terminal window and typing @q{guile}.  On some systems,
40 notably Windows, you may need to set the environment variable
41 @code{GUILE_LOAD_PATH} to the directory @code{../usr/shr/guile/1.8}
42 in the LilyPond installation (for the full path to this directory
43 see @rlearning{Other sources of information}).  Alternatively, Windows
44 users may simply choose @q{Run} from the Start menu and enter
45 @q{guile}.
46
47 @menu
48 * Introduction to Scheme::
49 * Scheme in LilyPond::
50 @end menu
51
52 @node Introduction to Scheme
53 @section Introduction to Scheme
54
55 We begin with an introduction to Scheme.  For this brief introduction,
56 we will use the GUILE interpreter to explore how the language works.
57 Once we are familiar with Scheme, we will show how the language can
58 be integrated in LilyPond files.
59
60
61 @menu
62 * Scheme simple data types::
63 * Scheme compound data types::
64 * Calculations in Scheme::
65 * Scheme procedures::
66 * Scheme conditionals::
67 @end menu
68
69 @node Scheme simple data types
70 @subsection Scheme simple data types
71
72 The most basic concept in a language is data typing: numbers, character
73 strings, lists, etc.  Here is a list of data types that are relevant to
74 LilyPond input.
75
76 @table @asis
77 @item Booleans
78 Boolean values are True or False.  The Scheme for True is @code{#t}
79 and False is @code{#f}.
80 @funindex ##t
81 @funindex ##f
82
83 @item Numbers
84 Numbers are entered in the standard fashion,
85 @code{1} is the (integer) number one, while @code{-1.5} is a
86 floating point number (a non-integer number).
87
88 @item Strings
89 Strings are enclosed in double quotes,
90
91 @example
92 "this is a string"
93 @end example
94
95 Strings may span several lines
96
97 @example
98 "this
99 is
100 a string"
101 @end example
102
103 Quotation marks and newlines can also be added with so-called escape
104 sequences.  The string @code{a said "b"} is entered as
105
106 @example
107 "a said \"b\""
108 @end example
109
110 Newlines and backslashes are escaped with @code{\n} and @code{\\}
111 respectively.
112 @end table
113
114
115 @node Scheme compound data types
116 @subsection Scheme compound data types
117
118 @unnumberedsubsubsec Pairs
119
120 TODO -- write about pairs
121
122 The two elements of a pair may be arbitrary values, for example
123
124 @example
125 #'(1 . 2)
126 #'(#t . #f)
127 #'("blah-blah" . 3.14159265)
128 @end example
129
130
131 @unnumberedsubsubsec Lists
132
133 TODO -- write about lists
134
135 A list is entered by enclosing its elements in parentheses, and adding
136 a quote.  For example,
137
138 @example
139 #'(1 2 3)
140 #'(1 2 "string" #f)
141 @end example
142
143 We have been using lists all along.  A calculation, like @code{(+ 1 2)}
144 is also a list (containing the symbol @code{+} and the numbers 1
145 and@tie{}2).  Normally lists are interpreted as calculations, and the
146 Scheme interpreter substitutes the outcome of the calculation.  To enter a
147 list, we stop the evaluation.  This is done by quoting the list with a
148 quote @code{'} symbol.  So, for calculations do not use a quote.
149
150 Inside a quoted list or pair, there is no need to quote anymore.  The
151 following is a pair of symbols, a list of symbols and a list of lists
152 respectively,
153
154 @example
155 #'(stem . head)
156 #'(staff clef key-signature)
157 #'((1) (2))
158 @end example
159
160 @unnumberedsubsubsec Hash tables
161
162 TODO -- write about hash tables
163
164
165 @node Calculations in Scheme
166 @subsection Calculations in Scheme
167
168 Scheme can be used to do calculations.  It uses @emph{prefix}
169 syntax.  Adding 1 and@tie{}2 is written as @code{(+ 1 2)} rather than the
170 traditional @math{1+2}.
171
172 @lisp
173 (+ 1 2)
174   @result{} 3
175 @end lisp
176
177 The arrow @result{} shows that the result of evaluating @code{(+ 1 2)}
178 is@tie{}@code{3}.  Calculations may be nested; the result of a function may
179 be used for another calculation.
180
181 @lisp
182 (+ 1 (* 3 4))
183   @result{} (+ 1 12)
184   @result{} 13
185 @end lisp
186
187 These calculations are examples of evaluations; an expression like
188 @code{(* 3 4)} is replaced by its value @code{12}.
189
190 The same assignment can be done in completely in Scheme as well,
191
192 @example
193 #(define twentyFour (* 2 twelve))
194 @end example
195
196 @c this next section is confusing -- need to rewrite
197
198 The @emph{name} of a variable is also an expression, similar to a
199 number or a string.  It is entered as
200
201 @example
202 #'twentyFour
203 @end example
204
205 @funindex #'symbol
206 @cindex quoting in Scheme
207
208 The quote mark @code{'} prevents the Scheme interpreter from substituting
209 @code{24} for the @code{twentyFour}.  Instead, we get the name
210 @code{twentyFour}.
211
212 @node Scheme procedures
213 @subsection Scheme procedures
214
215 @unnumberedsubsubsec Predicates
216
217 @unnumberedsubsubsec Return values
218
219 TODO -- write about scheme procedures
220
221 @node Scheme conditionals
222 @subsection Scheme conditionals
223
224 @unnumberedsubsubsec if
225
226 @unnumberedsubsubsec cond
227
228
229 @node Scheme in LilyPond
230 @section Scheme in LilyPond
231
232
233 @menu
234 * LilyPond Scheme syntax::
235 * LilyPond variables::
236 * Object properties::
237 * LilyPond compound variables::
238 @end menu
239
240 @node LilyPond Scheme syntax
241 @subsection LilyPond Scheme syntax
242
243 In a music file, snippets of Scheme code are introduced with the hash
244 mark @code{#}.  So, the previous examples translated to LilyPond are
245
246 @example
247 ##t ##f
248 #1 #-1.5
249 #"this is a string"
250 #"this
251 is
252 a string"
253 @end example
254
255 Note that LilyPond comments (@code{%} and @code{%@{ %@}}) cannot
256 be used within Scheme code.  Comments in Guile Scheme are entered
257 as follows:
258
259 @example
260 ; this is a single-line comment
261
262 #!
263   This a (non-nestable) Guile-style block comment
264   But these are rarely used by Schemers and never in
265   LilyPond source code
266 !#
267 @end example
268
269 Multiple consecutive scheme expressions in a music file can be
270 combined using the @code{begin} operator. This permits the number
271 of hash marks to be reduced to one.
272
273 @example
274 #(begin
275   (define foo 0)
276   (define bar 1))
277 @end example
278
279 @c todo -- # introduces a scheme *expression*
280 @c         need the concept of an expression
281
282 If @code{#} is followed by an opening parenthesis, @code{(}, as in
283 the example above, the parser will remain in Scheme mode until
284 a matching closing parenthesis, @code{)}, is found, so further
285 @code{#} symbols to introduce a Scheme section are not required.
286
287 For the rest of this section, we will assume that the data is entered
288 in a music file, so we add @code{#}s everywhere.
289
290 @node LilyPond variables
291 @subsection LilyPond variables
292
293
294 TODO -- make this read right
295
296 A similar thing
297 happens with variables.  After defining a variable
298
299 @example
300 twelve = 12
301 @end example
302
303 @noindent
304 variables can also be used in expressions, here
305
306 @example
307 twentyFour = (* 2 twelve)
308 @end example
309
310 @noindent
311 the number 24 is stored in the variable @code{twentyFour}.
312
313 @node Object properties
314 @subsection Object properties
315
316 This syntax will be used very frequently, since many of the layout
317 tweaks involve assigning (Scheme) values to internal variables, for
318 example
319
320 @example
321 \override Stem #'thickness = #2.6
322 @end example
323
324 This instruction adjusts the appearance of stems.  The value @code{2.6}
325 is put into the @code{thickness} variable of a @code{Stem}
326 object.  @code{thickness} is measured relative to the thickness of
327 staff lines, so these stem lines will be @code{2.6} times the
328 width of staff lines.  This makes stems almost twice as thick as their
329 normal size.  To distinguish between variables defined in input files (like
330 @code{twentyFour} in the example above) and variables of internal
331 objects, we will call the latter @q{properties} and the former
332 @q{variables.}  So, the stem object has a @code{thickness} property,
333 while @code{twentyFour} is an variable.
334
335 @cindex properties vs. variables
336 @cindex variables vs. properties
337
338 @c  todo -- here we're getting interesting.  We're now introducing
339 @c  LilyPond variable types.  I think this deserves a section all
340 @c  its own
341
342 @node LilyPond compound variables
343 @subsection LilyPond compound variables
344
345 @unnumberedsubsubsec Offsets
346
347 Two-dimensional offsets (X and Y coordinates) as well as object sizes
348 (intervals with a left and right point) are entered as @code{pairs}.  A
349 pair@footnote{In Scheme terminology, the pair is called @code{cons},
350 and its two elements are called @code{car} and @code{cdr} respectively.}
351 is entered as @code{(first . second)} and, like symbols, they must be quoted,
352
353 @example
354 \override TextScript #'extra-offset = #'(1 . 2)
355 @end example
356
357 This assigns the pair (1, 2) to the @code{extra-offset} property of the
358 TextScript object.  These numbers are measured in staff-spaces, so
359 this command moves the object 1 staff space to the right, and 2 spaces up.
360
361 @unnumberedsubsubsec Extents
362
363 todo -- write something about extents
364
365 @unnumberedsubsubsec Property alists
366
367 todo -- write something about property alists
368
369 @unnumberedsubsubsec Alist chains
370
371 todo -- write something about alist chains
372
373 @ignore
374 @menu
375 * Tweaking with Scheme::
376 @end menu
377
378 @node Tweaking with Scheme
379 @section Tweaking with Scheme
380
381 We have seen how LilyPond output can be heavily modified using
382 commands like
383 @code{\override TextScript #'extra-offset = ( 1 . -1)}.  But
384 we have even more power if we use Scheme.  For a full explanation
385 of this, see the @ref{Scheme tutorial}, and
386 @ref{Interfaces for programmers}.
387
388 We can use Scheme to simply @code{\override} commands,
389
390 TODO Find a simple example
391 @c This isn't a valid example with skylining
392 @c It works fine without padText  -td
393 @end ignore
394
395 @ignore
396 @lilypond[quote,verbatim,ragged-right]
397 padText = #(define-music-function (parser location padding) (number?)
398 #{
399   \once \override TextScript #'padding = #$padding
400 #})
401
402 \relative c''' {
403   c4^"piu mosso" b a b
404   \padText #1.8
405   c4^"piu mosso" d e f
406   \padText #2.6
407   c4^"piu mosso" fis a g
408 }
409 @end lilypond
410 @end ignore
411
412 @ignore
413 We can use it to create new commands:
414
415 @c Check this is a valid example with skylining
416 @c It is - 'padding still works
417
418
419 @lilypond[quote,verbatim,ragged-right]
420 tempoPadded = #(define-music-function (parser location padding tempotext)
421   (number? string?)
422 #{
423   \once \override Score.MetronomeMark #'padding = $padding
424   \tempo \markup { \bold $tempotext }
425 #})
426
427 \relative c'' {
428   \tempo \markup { "Low tempo" }
429   c4 d e f g1
430   \tempoPadded #4.0 #"High tempo"
431   g4 f e d c1
432 }
433 @end lilypond
434
435
436 Even music expressions can be passed in:
437
438 @lilypond[quote,verbatim,ragged-right]
439 pattern = #(define-music-function (parser location x y) (ly:music? ly:music?)
440 #{
441   $x e8 a b $y b a e
442 #})
443
444 \relative c''{
445   \pattern c8 c8\f
446   \pattern {d16 dis} { ais16-> b\p }
447 }
448 @end lilypond
449 @end ignore