# Conditionals

## Exercises

### Exercise 0: Preparation

Read the reading on conditionals. You may also want to rescan the reading on numbers and reading on Boolean values, which includes information on predicates.

### Exercise 1: What is it?

Define and test a Scheme predicate `atom-or-list?` that takes one argument and returns `#t` if the argument is either an atom (symbol) or a list, `#f` if it is neither.

### Exercise 2: Between, Revisited

Define and test a Scheme predicate `between?` that takes three arguments, all real numbers, and determines whether the second one lies strictly between the first and third (returning `#t` if it is, `#f` if it is not). For example, 6 lies strictly between 5 and 13, so both `(between? 5 6 13)` and `(between? 13 6 5)` should have the value `#t`.

### Exericse 3: Triangulation

Three line segments can be assembled into a triangle if, and only if, the length of each of them is less than the sum of the lengths of the other two. Define a Scheme predicate `triangle?` that takes three arguments, all positive real numbers, and determines whether line segments of those three lengths (assumed to be measured in the same units) could be assembled into a triangle.

### Exercise 4: Being Neighborly

Define and test a Scheme procedure `neighbor` that takes one argument, an integer, and returns the next higher integer if its argument is even, the next lower integer if its argument is odd. (Start by writing a comment that describes the purpose of the procedure.)

### Exercise 5: Non-Boolean Tests

For each of the following expressions, guess what the output should be and then test it in Scheme.

• `(if #t 'aardvark 'zebra)`
• `(if #f 'aardvark 'zebra)`
• `(if (null? null) 'aardvark 'zebra)`
• `(if (null? (list 'a 'b 'c)) 'aardvark 'zebra`
• `(if () 'aardvark 'zebra)`
• `(if (list 'a 'b 'c) 'aardvark 'zebra)`
• `(if 2 'aardvark 'zebra)`
• `(if 'true 'aardvark 'zebra)`
• `(if 'false 'aardvark 'zebra)`

### Exercise 6: Categorizing Lists

Define and test a Scheme procedure `list-type` that takes one argument, a list, and returns the symbol `empty` if the argument is the empty list, the symbol `non-empty` otherwise.

### Exercise 7: Who Won?

Define and test a Scheme procedure `report-victory` that takes one argument, a real number, and returns the string `"I won!"` if that number is positive, the string `"You won!"` if it is negative, and the string `"It's a tie!"` if it is zero.

a. Use `if` for all your tests.

b. Use `cond` for all your tests.

### Exercise 8: The Sphinx's Riddle

As you may know, one of the famous riddles of the Sphinx goes something like the following:

What is it that walks upon four legs, then two legs, then three legs?

Write a Scheme predicate, `legs`, that, given someone's age, tells how many legs they walk upon. (You get to choose reasonable ages for the three phases of life.)

### Exercise 9: Categorizing Data

Write and test a Scheme procedure, `type`, that returns the a symbol that represents the type of its argument. For example,

```> (type 3)
integer
> (type '(1 2 3))
list
> (type "hello")
string
> (type 2/3)
rational
> (type 'type)
symbol
> (type type)
procedure
```

## History

Wednesday, 6 September 2000

Friday, 8 September 2000

• Fixed some problems.
• Added new exercise on classifying data.