CS 1410-20 Homework 3

Due: Friday, September 16th, 2011 10:45am

The next step for Mildly Annoyed Ducks is to have a list of bricks for a flying duck to destroy. We’ll ease into that problem, however, with a set of warm-up exercises using just lists of numbers:

  ;; A list-of-number is either
  ;;   - empty
  ;;   - (cons string list-of-number)

The list of numbers represents vertical locations for bricks that are each 30 pixels high. For example, (cons 10 (cons 70 (cons 40 empty))) represents three bricks that form a solid wall spanning position 10 through 100.

Part 1 – Wall-Height

Write the function wall-height, which takes a list of numbers representing bricks and returns the maximum position covered by the wall’s bricks (i.e., 30 more than the maximum number in the list). Assume that all the bricks have a zero or positive location, and if the wall has no bricks, the result should be 0. You’ll probably want to use the max function.

For example, (wall-height (cons 10 (cons 70 (cons 40 empty)))) should produce 100.

Part 2 – Aligned-Bricks?

Write the function aligned-bricks?, which takes a list of numbers and returns true if the lists contains only integers, false otherwise. You’ll probably want to use the integer? function.

For example, (aligned-bricks? (cons 10 (cons 70 (cons 40 empty))) should produce true, while (aligned-bricks? (cons 10.2 empty)) should produce false.

Note that every number in the empty list is an integer, since there are no numbers in the list that have a fractional part.

Part 3 – Align-Bricks

Write the function align-bricks, which takes a list of numbers and returns a list of the same length, but where every number in the list is rounded to an integer. You’ll probably want to use the round function.

For example, (align-bricks (cons 10.1 (cons 70 (cons 39.6 empty)))) should produce (cons 10 (cons 70 (cons 40 empty))).

Part 4 – Search-and-Delete

Write the function search-and-delete, which takes a number and a list of numbers. The search-and-delete function returns a list like the one that it is given, except that any brick that touches the given position (i.e., a number that is <= the given one and whose sum with 30 is >= the given one) is removed from the list.

For example, (search-and-delete 40 (cons 10 (cons 70 (cons 40 empty)))) should produce (cons 70 empty).

Part 5 – Insert

Write the function insert, which takes a number to insert, a position (as a number) to insert the new number, and a list of numbers. The insert function returns a list like the one that it is given, except that the number to insert is added after the number of elements indicated by the position. If the position is beyond the end of the list (i.e., it is more than the number of number in the list), then add the new number to the end of the list.

For example, (insert 40 5 empty) should produce (cons 40 empty), and (insert 40 2 (cons 10 (cons 70 (cons 100 empty)))) should produce (cons 10 (cons 70 (cons 40 (cons 100 empty)))).


Last update: Friday, November 4th, 2011
mflatt@cs.utah.edu