Conditional evaluation

Course links

Exercise 1

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 2

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

Exercise 3

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.

Exercise 4

Define a Scheme procedure classify-by-age that takes one argument, a non-negative integer, and returns the symbol infant if the argument is 0 or 1, the symbol child if the argument is less than or equal to 12, the symbol adolescent if the argument is less than or equal to 18, the symbol adult if the argument is less than 65, and the symbol senior in any other case. Test your procedure by calling it with a representative age within each classification.

Exercise 5

Define and test a Scheme predicate string-or-symbol? that takes one argument and returns #t if the argument is either a string or a symbol, #f if it is neither.

Exercise 6

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.

Exercise 7

A natural number is an integer that is not negative. Furthermore, in Scheme programs, we'll apply this term only to non-negative integers that are represented exactly, not to approximations. (The built-in Scheme predicate exact? can be used to test whether a number is represented exactly.) Define and test a Scheme predicate natural-number? that determines whether its argument is a natural number.

Exercise 8

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.

I am indebted to Professor Ben Gum for his contributions to the development of this lab.