Teach Yourself Scheme: 4.2 cond

(idea) by washort Tue Apr 18 2000 at 4:01:28
Prev Up Next

The cond form is convenient for expressing nested if-expressions, where each ``else'' branch but the last introduces a new if. Thus, the form

(if (char<? c #\c) -1
    (if (char=? c #\c) 0
        1))

can be rewritten using cond as:

(cond ((char<? c #\c) -1)
      ((char=? c #\c) 0)
      (else 1))

The cond is thus a multi-branch conditional. Each clause has a test and an associated action. The first test that succeeds triggers its associated action. The final else clause is chosen if no other test succeeded.

The cond actions are implicit begins.

Prev Up Next

Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.