Nothing to Teach

Chapter 1.1.6 - Conditional Expressions and Predicates

This chapter covers three different ways of doing case analysis: cond, if, and and/or/not.

cond stands for “conditional”. An example of using cond looks like this:

1(define (abs x)
2    (cond ((> x 0) x)
3          ((= x 0) 0)
4          ((< x 0) (- x))))

They state that the “general form of a conditional expression is”:

(cond (⟨p1⟩ ⟨e1⟩)
      (⟨p2⟩ ⟨e2⟩)
      . . .
      (⟨pn⟩ ⟨en⟩))

p stands for predicate, and e stands for expression.

In a cond expression, each predicate is evaluated, one at a time. If the first predicate is found to be false, it moves on to the second predicate and so on.

If it finds a true predicate, it evaluates and returns the value of the associated expression. For example, if x is 3, then the cond would stop there because 3 is greater than 0.

In English:

cond ((x       >         0)        x)
 |     |       |         |         |
If     3 is greater than 0, return 3

Equivalent ways of saying the same thing are:

1(define (abs x)
2    (cond ((< x 0) (- x))
3          (else x)))

Also:

1(define (abs x)
2    (if (< x 0)
3        (- x)
4        x))

In English: if x is less than 0, return -x, else return x.

The “logical composition operations” are and, or, and not.

and returns true if all of its arguments are true. or returns true if at least one of its arguments is true. not returns the opposite of its argument.

Soundtrack: The Weight by Weval

Tags: books sicp