| PSICS Fall 2004 Quiz #5 |
|   PSICS Home   |   Description | Extra Credit | Submitting |
Submit your solutions to this quiz by dropping in your webct drop box in the box labeled "Quiz5". Let Dave know if you have problems submitting!
Note: Some of the descriptions below use the
list primitive operation to describe lists rather than
using nested cons expressions.
| Assignment |
You need to create a function called
generate_numbers that will create a list of integers
based on the 3 parameters: start, end and
interval. The function should generate a list of numbers
starting with the number start, each successive
number number should be closer to the number end and the
numbers should all be interval apart. For example, the
numbers from 1 to 10 by 2 would be 1,3,5,7,9. Below are some sample
calls of your function and the correct results:
(generate_numbers 0 5 1) => (cons 0 (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 empty)))))) (generate_numbers 10 5 1) => (cons 10 (cons 9 (cons 8 (cons 7 (cons 6 (cons 5 empty)))))) (generate_numbers 0 10 3) => (cons 0 (cons 3 (cons 6 (cons 9 empty)))) (generate_numbers 100 1000 222) => (cons 100 (cons 322 (cons 544 (cons 766 (cons 988 empty))))) (generate_numbers 1 10 25) => (cons 1 empty)
IMPORTANT: Your code must be able to handle counting up (start < end) and counting down (start > end).
Another way to think about this function is that it counts by
interval starting at start and stopping
before it passes end. For example,
(generate_numbers 0 10 2) counts by 2s starting at 0 and
ending when it reaches 10.
You need to create a function called
merge-sorted-lons that will take two lists of numbers,
each of which is sorted in ascending order. The function produces a new list
that contains all the elements from both lists combined in a single,
ordered (ascending order) list. Your function should assume that both
lists it gets are actually ordered, but either of these could be an
empty list and either list can contain duplicate elements.
(define even (list 0 2 4 6 8)) (define odd (list 1 3 5 7 9)) (define spread (list 1 100 200 300)) (merge-sorted-lons even odd) => (cons 0 (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 (cons 6 (cons 7 (cons 8 (cons 9 empty)))))))))) (merge-sorted-lons odd even) => (cons 0 (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 (cons 6 (cons 7 (cons 8 (cons 9 empty)))))))))) (merge-sorted-lons even empty) => (cons 0 (cons 2 (cons 4 (cons 6 (cons 8 empty))))) (merge-sorted-lons odd spread) => (cons 1 (cons 1 (cons 3 (cons 5 (cons 7 (cons 9 (cons 100 (cons 200 (cons 300 empty)))))))))
You need to create a function that processes lists in the following
way: it assumes that a list describes an arithmetic operation
involving two numbers and an operation name. The operation name is the
second list item, so that one operation looks something like this:
(list 12 'plus 13) (represents 12+13).
Your function will interpret such lists by doing the computation described by the list. You only need to support 4 different operations:
'plus (addition)'minus (subtraction)'times (multiplication)'div (division)Your function must generate an error (use the error
function!), if any other operation is found.
Below are some simple example of how your function might be used to do some computations:
(interp (list 12 'plus 13)) => 25 (interp (list 85 'div 5)) => 17 (interp (list 3 'times 2)) => 6 (interp (list 33 'blah 33)) => interp: Unknown operator
For the simple operations shown above, the list given to your function contains a number, a symbol (the operation) and another number. Your function must also be able to handle more complex expressions in which it gets a list instead of a number. For example, the expression 4*3 + 5 would look like this:
(list (list 4 'times 3) 'plus 5)
Here are some other examples:
(interp
(list 12 'plus (list 14 'minus 3))) => 23
(interp
(list
(list 3 'times 4)
'plus
(list 10 'div 5))) => 14
Make sure your interp function can handle any kind of
list in which the second element is one of the symbols described above
('plus, 'times, 'minus or 'div), and the first/third list elements can
either be a number or another list. Think Recursion! (you may find it
useful to write a helper function).
| Submitting |
Submit your code as a single program (file) to the WebCT Drop Box labeled "Quiz 5". We should be able to easily find the required functions and to test them out (please give us some test code!).