Conditional evaluation

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

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.


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~gum/courses/151/labs/conditional-evaluation.xhtml

created September 9, 1997
last revised July 31, 2001

John David Stone (stone@cs.grinnell.edu) and Ben Gum (gum@cs.grinnell.edu)