if syntax#

Conditions allow to take decisions while executing a program, like „if temperature is higher than 20 °C, display that the weather is sunny“. The syntax is:

(if condition expression-if-yes expression-if-no)

Unlike many languages where if is an instruction which executes code if the condition is met, Scheme’s if is an expression. The essential difference is that an expression evaluates to a value, while an instruction performs an action without returning a value. Again, you have to reframe your mind to think a little differently: instead of „if the temperature is higher than 20°C, assign sunny to weather, else, assign gloomy to weather“, you will typically say this in Scheme: „let weather be the value that is sunny if the temperature is higher than 20°C and gloomy otherwise“. Here is a first example:

(define (equal-or-different x y)
    (if (= x y)
        "x and y are equal."
        "x and y are different."))

(equal-or-different 4 4)
 x and y are equal.
(equal-or-different 4 5)
 x and y are different.

As you likely guessed, the = procedure tests whether two numbers are equal. What kind of value does it return?

(= 4 5)

This test was evaluated to #f, which is the Scheme notation for the boolean „false“. The opposite of #f is #t („true“).

(= 5 5)

Importantly, only one of the two expressions is evaluated, depending on the value of the test. The previous example could also have been written like this:

(if (= x y)
    (display "x and y are equal.")
    (display "x and y are different."))

If \(x \neq y\), the expression (display "x and y are equal.") is never evaluated, which is fortunate, since otherwise both messages would be printed on the console. Another advantage is that this allows programs such as:

(define (display-inverse x)
  (if (= x 0)
      (display "can't divide by zero")
      (display (/ 1 x)))

(display-inverse 0)
 can't divide by zero

This function prints the inverse of its argument \(x\), i.e. \(\frac1x\). This inverse only exists if \(x \neq 0\), as you can’t divide by 0. Had if evaluated both of its arguments, the expression (/ 1 x) would have been evaluated even if \(x = 0\), causing an error.

cond syntax#

cond is an equivalent to what is written in other languages as if ... else if ... else if .... This syntax is a practical way to write code that needs to branch between many possible cases. Imagine that you are coding the part of LilyPond that prints a note head depending on its style. You could write something like this:

(if (style-is-default)
    ;; Else ...
    (if (style-is-altdefault)
        ;; Else ...
        (if (style-is-baroque)
            ;; Else ...
            (if (style-is-mensural)
                ;; Else ...
                (if (style-is-petrucci)
                    ;; Else ...
                    etc. etc. etc.)))))

Obviously, this is tedious and there are excessively many levels of nesting. This is where cond comes in handy. Its syntax is:

  (condition1 expression1)
  (condition2 expression2)
  (condition3 expression3)

The conditions are evaluated in order. As soon as one is true, the corresponding expression is evaluated, and the entire cond expression takes its value.

In the place of the last condition, you can write else. This is the same as writing #t. The last expression is then always evaluated in case no other condition was true.

Here is an example using cond:

(define (category temperature)
   ((> temperature 50)
    "unbearable temperature")
   ((> temperature 40)
    "hardly bearable temperature")
   ((> temperature 30)
    "hot to very hot temperature")
   ((> temperature 20)
    "moderate temperature")
   ((> temperature 10)
    "rather cold temperature")
   ((> temperature 0)
    "cold temperature")
   ((> temperature -10)
    "very cold temperature")
    "polar temperature")))

(display (category 28))
 moderate temperature

Frequently used tests#

The first test you met was =. It applies exclusively to numbers. It tests their numeric equality, i.e., it does not take into account whether they are integer or floating-point numbers.

(= 3 3.0)

Another frequently used equality test is equal?. It applies to all data types, not just numbers. For example, you can use it to compare strings.

(equal? "Hello" "Hello")

Even on numbers, = and equal? are not equivalent, since equal? does not consider numbers of different types as equal.

(equal? 3 3.0)

The question mark is part of the name of equal?. Syntactically, it is no problem in Scheme to have a question mark in the name of a variable (see Defining variables). By convention, a question mark is used at the end of the name of any procedure that performs a test, returning true or false. Such procedures are called „predicates“.

To compare numbers, there are procedures <, <=, > and >=, which are read „less than“, „less than or equal“, „greater than“ and „greater than or equal“. They are equivalent to the mathematical symbols \(<\), \(\leq\), \(>\) and \(\geq\).

(>= 4 3)
(>= 3 4)
(> 3 3)
(>= 3 3)

Combining tests#

There are two main logical operators to combine tests together:

  • and checks whether all conditions are true;

  • or checks whether at least one of the conditions is true.

(define (GPS location)
  (if (or (equal? location "North Pole")
          (equal? location "South Pole"))
      (display "Put on your coat!")
      (display "Not at the pole yet."))

(GPS "North Pole")
 Put on your coat!
(GPS "Helsinki")
 Not at the pole yet.
(define (detect-requiem composer work)
    ((and (equal? work "Requiem")
          (equal? composer "Mozart"))
     (display "Mozart's Requiem"))
    ((equal? work "Requiem")
     (display "A Requiem (not Mozart's)"))
     (display "Not a Requiem"))))

(detect-requiem "Mozart" "Requiem")
 Mozart's Requiem

The not operator inverts the result of a test.

(define (conversation is-musician preferred-software)
    ((not is-musician)
     (display "What a pity for you!"))
    ((not (equal? preferred-software "LilyPond"))
     (display "Do you know a fantastic piece of software called LilyPond?"))
     (display "How about starting to contribute?")))

(conversation #t "LilyPond")
 How about starting to contribute?

One-armed if#

Quite often, there is nothing to do if a condition is not met. This is particularly true when what is done if the condition is true is printing an error message. For these cases, there is a second form of if, where the expression to be evaluated if the condition is false is simply omitted.

(if condition expression-if-yes)

For example:

(define (check-composer name)
  (if (equal? name "unknown")
      (error "composer not found in the database")))

(check-composer "unknown")

If the condition is true, expression-if-yes is evaluated, and the if takes its value, as in a usual if. If, on the other hand, the condition is false, the expression has no interesting value. Actually, it prints nothing in the sandbox:

guile> (if #t "value if yes")
"value if yes"
guile> (if #f "value if yes")

Still, the expression does have a value!

guile> *unspecified*
guile> (equal? *unspecified* (if #f "value if yes"))

This value is *unspecified*, a special constant returned by all functions that do not need to return any special value. The display function also returns *unspecified*. In order not to clutter the interactive session, the sandbox simply ignores *unspecified*.

On universal truth#

if expressions do not accept only booleans as conditions. In many languages other than Scheme, certain types have their own rules regarding truthiness. Typically, numbers would be true except if 0, and strings or lists would be true except if empty.

Scheme is quite different. Absolutely all values are true, except the boolean #f. In particular, 0 and empty strings are true.

(if 0 "yes" "no")

This is why the value #f is usually used to represent missing values, unfound values, etc. With this convention, the check-composer can be rewritten as:

(define (check-composer name) ; name is a string or #f
  (if (not name) ; detects #f
      (error "composer not found in database")))

Just like if, and and or are lazy: they only evaluate their arguments when it turns out necessary. Also, they do not necessarily return a boolean. When all of its arguments are true, and returns the value of the last argument. When one of its arguments is true, or returns the first such argument. This principle is what gives rise to a common trick for defining default values.

(define (display-composer composer)
   (or composer "[unknown]"))

(display-composer "Mozart")
(display-composer #f)

Let us analyze what happens step-by-step. If composer is a true value (namely anything but #f), the test (or composer "[unknown"]) can stop executing, as or has already found a true value (just one is needed). It is then this value that or returns. On the other hand, if composer is #f, the or test cannot stop immediately and evaluates the next expression, "[unknown]". This expression is true, so or stops and returns it.