# CS 536 Homework 1: Warming up in Scheme

### Assignment Goals

• To make sure you can write simple programs in Scheme.

### Exercises

1. Define the function suffixes, which consumes a list l, and produces a list of all suffixes of l. For example:

```    (suffixes '(a b c d))
> ((a b c d) (b c d) (c d) (d) ())
```
2. Develop a program isort to sort a list of numbers into increasing order. You may use any algorithm you wish (efficiency doesn't matter, but cleanliness of your solution does).

3. Define a datatype for toys. A toy is one of:

• a ball, which has a price
• a (stuffed) animal, which has a price, and an animal type (tiger, bear, etc)
• a robot, which has a price, a height, and whether it is programmable.

4. Develop the function bear-sale that consumes a list of toys and returns a list of toys. The returned list has the same toys as the input list, but the prices on all bears have been discounted 20%.

5. Develop the function eliminate-exp to eliminate expensive toys. The function consumes a price (number) and a list of toys and produces a list of all those toys that cost no more than the given price.

6. Develop the function tall-programmable? that consumes a list of toys and returns a boolean indicating whether the list contains a programmable robot more than 3 feet tall.

7. Define a datatype for a family tree. A family tree is either:

• unknown, or
• a person, which has five fields:
• name, which is a string
• birthyear, which is a number
• eyecolor, which is a symbol
• father and mother, which are family trees

For example, a small family tree looks like:

```
(person "Dave" 1977 'brown
(person "Ken" 1945 'brown
(unknown)
(unknown))
(person "Mary Ellen" 1946 'brown
(unknown)
(unknown)))
```

8. Develop count-persons. The function consumes a family tree node and produces the number of people in the corresponding family tree.

9. Develop the function average-age. It consumes a family tree node and the current year. It produces the average age of all people in the family tree. You may assume that the given tree contains at least one person.

10. Develop the function eye-colors, which consumes a family tree node and produces a list of all eye colors in the tree. Each eye color should occur at most once in the list.

Hint: Use the Scheme operation append, which consumes two lists and produces the concatenation of the two lists. For example:

`(append (list 'a 'b 'c) (list 'd 'e)) = (list 'a 'b 'c 'd 'e)`

Back to the Assignments page