<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <!-- header_tag --> <html lang="en"> <head> <title>Scheme tutorial - GNU LilyPond Learning Manual</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="description" content="GNU LilyPond Learning Manual"> <meta name="generator" content="makeinfo 4.11"> <link title="Top" rel="start" href="index.html#Top"> <link rel="prev" href="Templates.html#Templates" title="Templates"> <link rel="next" href="GNU-Free-Documentation-License.html#GNU-Free-Documentation-License" title="GNU Free Documentation License"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <!-- Copyright (C) 1999--2007 by the authors Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections. A copy of the license is included in the section entitled ``GNU Free Documentation License''. --> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } hr { border:0; height:1; color: #000000; background-color: #000000; } /* hr { border: none; height: 1px; color: #666666; background-color: #666666; } body { border-left: 1px solid #666666; border-right: 1px solid #666666; color: #332d28; margin-right: auto; margin-left: auto; width: 60em; list-style-type: square; font-family: Arial,Helvetica,sans-serif; padding-right: 1em; padding-left: 1em; } a { border-bottom: 1px dashed #344242; text-decoration: none; color: #344242; } a:link { text-decoration: none; } a:visited { border-bottom: 1px dashed #666666; color: #666666; } a:active { border-bottom: 1px solid #00cccc; color: #00cccc; } a:hover { border-bottom: 1px solid #1d7b85; color: #1d7b85; } blockquote { border: 1px solid #cccccc; padding: 3px; width: 40em; } .node { border-left: 1px solid #666666; margin: -0.5em 0px 1em; padding: 2px 1px 0px; font-style: italic; } .node a { border: none; text-decoration: underline; font-style: normal; font-weight: bold; } .verbatim { font-family: "Courier New",Courier,monospace; } .unnumberedsubsubsec { font-size: large; color: #1d7b85; } .subsubheading { font-size: large; color: #3b220d; } .contents { border: 1px dashed #339999; margin: 3px 2em; list-style-type: square; padding-right: 1em; width: 40em; background-color: #fcfff9; } .contents a { border-bottom: 1px dashed #423d34; text-decoration: none; color: #423d34; } .contents a:visited { border-bottom: 1px dashed #666666; color: #666666; } .contents a:active { border-bottom: 1px solid #f0d86d; color: #f0d86d; } .contents a:hover { border-bottom: 1px solid #3b220d; color: #3b220d; } .menu { border-left: 1px dashed #339999; margin: 3px 2em 1em; list-style-type: square; padding-left: 1.4em; width: 40em; } .unnumbered { } h2 { font-size: x-large; color: #1d7b85; } */ --></style> </head> <BODY BGCOLOR=WHITE TEXT=BLACK> <div class="node"> <p> <a name="Scheme-tutorial"></a> Next: <a rel="next" accesskey="n" href="GNU-Free-Documentation-License.html#GNU-Free-Documentation-License">GNU Free Documentation License</a>, Previous: <a rel="previous" accesskey="p" href="Templates.html#Templates">Templates</a>, Up: <a rel="up" accesskey="u" href="index.html#Top">Top</a> <hr> </div> <h2 class="appendix">Appendix B Scheme tutorial</h2> <p><a name="index-g_t_0023-447"></a><a name="index-g_t_0023-448"></a> <a name="index-Scheme-449"></a><a name="index-GUILE-450"></a><a name="index-Scheme_002c-in_002dline-code-451"></a><a name="index-accessing-Scheme-452"></a><a name="index-evaluating-Scheme-453"></a><a name="index-LISP-454"></a> LilyPond uses the Scheme programming language, both as part of the input syntax, and as internal mechanism to glue modules of the program together. This section is a very brief overview of entering data in Scheme. If you want to know more about Scheme, see <a href="http://www.schemers.org">http://www.schemers.org</a>. <p>The most basic thing of a language is data: numbers, character strings, lists, etc. Here is a list of data types that are relevant to LilyPond input. <dl> <dt>Booleans<dd>Boolean values are True or False. The Scheme for True is <code>#t</code> and False is <code>#f</code>. <a name="index-g_t_0023_0023t-455"></a><a name="index-g_t_0023_0023t-456"></a> <a name="index-g_t_0023_0023f-457"></a><a name="index-g_t_0023_0023f-458"></a> <br><dt>Numbers<dd>Numbers are entered in the standard fashion, <code>1</code> is the (integer) number one, while <code>-1.5</code> is a floating point number (a non-integer number). <br><dt>Strings<dd>Strings are enclosed in double quotes, <pre class="example"> "this is a string" </pre> <p>Strings may span several lines <pre class="example"> "this is a string" </pre> <p>Quotation marks and newlines can also be added with so-called escape sequences. The string <code>a said "b"</code> is entered as <pre class="example"> "a said \"b\"" </pre> <p>Newlines and backslashes are escaped with <code>\n</code> and <code>\\</code> respectively. </dl> <p>In a music file, snippets of Scheme code are introduced with the hash mark <code>#</code>. So, the previous examples translated in LilyPond are <pre class="example">##t ##f #1 #-1.5 #"this is a string" #"this is a string" </pre> <p>For the rest of this section, we will assume that the data is entered in a music file, so we add <code>#</code>s everywhere. <p>Scheme can be used to do calculations. It uses <em>prefix</em> syntax. Adding 1 and 2 is written as <code>(+ 1 2)</code> rather than the traditional 1+2. <pre class="lisp">#(+ 1 2) #3 </pre> <p>The arrow shows that the result of evaluating <code>(+ 1 2)</code> is <code>3</code>. Calculations may be nested; the result of a function may be used for another calculation. <pre class="lisp">#(+ 1 (* 3 4)) #(+ 1 12) #13 </pre> <p>These calculations are examples of evaluations; an expression like <code>(* 3 4)</code> is replaced by its value <code>12</code>. A similar thing happens with variables. After defining a variable <pre class="example">twelve = #12 </pre> <p class="noindent">variables can also be used in expressions, here <pre class="example">twentyFour = #(* 2 twelve) </pre> <p class="noindent">the number 24 is stored in the variable <code>twentyFour</code>. The same assignment can be done in completely in Scheme as well, <pre class="example">#(define twentyFour (* 2 twelve)) </pre> <p>The <em>name</em> of a variable is also an expression, similar to a number or a string. It is entered as <pre class="example">#'twentyFour </pre> <p><a name="index-g_t_0023_0027symbol-459"></a><a name="index-g_t_0023_0027symbol-460"></a> <a name="index-quoting-in-Scheme-461"></a> The quote mark <code>'</code> prevents the Scheme interpreter from substituting <code>24</code> for the <code>twentyFour</code>. Instead, we get the name <code>twentyFour</code>. <p>This syntax will be used very frequently, since many of the layout tweaks involve assigning (Scheme) values to internal variables, for example <pre class="example">\override Stem #'thickness = #2.6 </pre> <p>This instruction adjusts the appearance of stems. The value <code>2.6</code> is put into the <code>thickness</code> variable of a <code>Stem</code> object. <code>thickness</code> is measured relative to the thickness of staff lines, so these stem lines will be <code>2.6</code> times the width of staff lines. This makes stems almost twice as thick as their normal size. To distinguish between variables defined in input files (like <code>twentyFour</code> in the example above) and variables of internal objects, we will call the latter ‘properties’ and the former ‘variables.’ So, the stem object has a <code>thickness</code> property, while <code>twentyFour</code> is an variable. <p><a name="index-properties-vs_002e-variables-462"></a><a name="index-variables-vs_002e-properties-463"></a> Two-dimensional offsets (X and Y coordinates) as well as object sizes (intervals with a left and right point) are entered as <code>pairs</code>. A pair<a rel="footnote" href="#fn-1" name="fnd-1"><sup>1</sup></a> is entered as <code>(first . second)</code> and, like symbols, they must be quoted, <pre class="example">\override TextScript #'extra-offset = #'(1 . 2) </pre> <p>This assigns the pair (1, 2) to the <code>extra-offset</code> property of the TextScript object. These numbers are measured in staff-spaces, so this command moves the object 1 staff space to the right, and 2 spaces up. <p>The two elements of a pair may be arbitrary values, for example <pre class="example">#'(1 . 2) #'(#t . #f) #'("blah-blah" . 3.14159265) </pre> <p>A list is entered by enclosing its elements in parentheses, and adding a quote. For example, <pre class="example">#'(1 2 3) #'(1 2 "string" #f) </pre> <p>We have been using lists all along. A calculation, like <code>(+ 1 2)</code> is also a list (containing the symbol <code>+</code> and the numbers 1 and 2). Normally lists are interpreted as calculations, and the Scheme interpreter substitutes the outcome of the calculation. To enter a list, we stop the evaluation. This is done by quoting the list with a quote <code>'</code> symbol. So, for calculations do not use a quote. <p>Inside a quoted list or pair, there is no need to quote anymore. The following is a pair of symbols, a list of symbols and a list of lists respectively, <pre class="example">#'(stem . head) #'(staff clef key-signature) #'((1) (2)) </pre> <ul class="menu"> <li><a accesskey="1" href="Tweaking-with-Scheme.html#Tweaking-with-Scheme">Tweaking with Scheme</a> </ul> <div class="footnote"> <hr> <h4>Footnotes</h4><p class="footnote"><small>[<a name="fn-1" href="#fnd-1">1</a>]</small> In Scheme terminology, the pair is called <code>cons</code>, and its two elements are called <code>car</code> and <code>cdr</code> respectively.</p> <hr></div> <!-- footer_tag --><div class="node"> <p> Next: <a rel="next" accesskey="n" href="GNU-Free-Documentation-License.html#GNU-Free-Documentation-License">GNU Free Documentation License</a>, Previous: <a rel="previous" accesskey="p" href="Templates.html#Templates">Templates</a>, Up: <a rel="up" accesskey="u" href="index.html#Top">Top</a> </div> <div style="background-color: #e8ffe8; padding: 2; border: #c0ffc0 1px solid;"> <p> <font size="-1"> This page is for LilyPond-2.11.57 (development-branch). <br> <address> Report errors to <a href="http://post.gmane.org/post.php?group=gmane.comp.gnu.lilypond.bugs">http://post.gmane.org/post.php?group=gmane.comp.gnu.lilypond.bugs</a>. </address> <br> Your <a href="http://lilypond.org/web/devel/participating/documentation-adding">suggestions for the documentation</a> are welcome. </font> </p> </div> <P> Other languages: <a href="Scheme-tutorial.es.html">espaƱol</a>, <a href="Scheme-tutorial.de.html">deutsch</a>. <BR> </P> </BODY></html>