ps1 1
ps1 1
ps1 1
Reading:
The purpose of the exercises below is to familiarize you with the basics of Scheme language and
interpreter. Spending a little time on simple mechanics now will save you a great deal of time over
the rest of the semester.
Write a Scheme procedure (interval-union i1 i2) that returns the union of the intervals
i1 and i2.
interval-union: Interval × Interval → Listof(Interval)
Examples: (interval-union ’(1 5) ’(2 6)) => ((1 6))
(interval-union ’(1 5) ’(2 4)) => ((1 5))
(interval-union ’(1 5) ’(5 5)) => ((1 5))
(interval-union ’(1 5) ’(15 25)) => ((1 5) (15 25))
(interval-union ’(5 5) ’(25 25)) => ((5 5) (25 25))
Comp 451, PROBLEM SET—Problem Set 1 2
Caution: Look carefully at the form of the return values. In the past, sometimes students
procedures have produced (1 6) instead of ((1 6)).
4. Write the procedures first, second, and third that pick out those corresponding parts of a
proper list. You do not have to handle the case where the list is too short to have the
requested part.
Examples: (first (a b c d e)) => a
(second (a b c d e)) => b
(third (a b c d e)) => c
5. Write the procedures nth(n lst) that pick out the nth element from the list. In case the
index given is out of range the procedure should print an error message.
Examples: (nth 1 (a b c d e)) => a
(nth 4 (a b c d e)) => d
(nth 9 (a b c d e)) => "error"
6. Write the procedure (make-vec-from-points p1 p2) that returns the vector that goes from
the point p1 to the point p2.
make-vec-from-points: Point × Point → Vector
Examples: (make-vec-from-points ’(1 3 4) ’(3 6 2)) => (2 3 -2)
7. Write the procedure (dot-product v1 v2) that returns the dot-product (scalar product) of
the two vectors v1 and v2.
dot-product: Vector × Vector → Number
Examples: (dot-product ’(1 2 3) ’(4 5 6)) => 32 ; This is 1*4 + 2*5 + 3*6
8. Write the procedure (magnitude v) that returns the magnitude of the vector v. So that we
do not have to worry about round-off error, the test cases will only use examples where the
magnitude of the vector is an integer.
vec-length: Vector => Number
Examples: (magnitude ’(3 4 12)) => 13
9. Write the procedure (distance p1 p2) that returns the distance from the point p1 to the
point p2. So that we do not have to worry about round-off error, the test cases will only use
examples where the returned value is an integer.
[Hint: You may want to call some previously-defined procedures in your definition
distance: Point × Point → Number
Examples: (distance ’(3 1 2) ’(15 -15 23)) => 29