1.1.5 Calculations in Scheme

Scheme can be used to do calculations. It uses prefix syntax. Adding 1 and 2 is written as (+ 1 2) rather than the traditional 1+2.

guile> (+ 1 2)
$23 = 3
guile>

Calculations may be nested; the result of a function may be used for another calculation.

guile> (+ 1 (* 3 4))
$24 = 13
guile>

These calculations are examples of evaluations; an expression like (* 3 4) is replaced by its value 12.

Scheme calculations are sensitive to the differences between integers and non-integers. Integer calculations are exact, while non-integers are calculated to the appropriate limits of precision:

guile> (/ 7 3)
$25 = 7/3
guile> (/ 7.0 3.0)
$26 = 2.3333333333333335
guile>

When the Scheme interpreter encounters an expression that is a list, the first element of the list is treated as a procedure to be evaluated with the arguments of the remainder of the list. Therefore, all operators in Scheme are prefix operators.

If the first element of a Scheme expression that is a list passed to the interpreter is not an operator or procedure, an error will occur:

guile> (1 2 3)
ice-9/boot-9.scm:1676:22: In procedure raise-exception:
Wrong type to apply: 1

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
guile [1]> ,q
guile>

Here you can see that the interpreter was trying to treat 1 as an operator or procedure, and it couldn’t. Hence the error is "Wrong type to apply: 1".

Therefore, to create a list we need to use the list operator, or to quote the list so that the interpreter will not try to evaluate it.

guile> (list 1 2 3)
$27 = (1 2 3)
guile> '(1 2 3)
$28 = (1 2 3)
guile>

This is an error that can appear as you are working with Scheme in LilyPond.


Extending LilyPond v2.27.0 (development-branch).