P3
Scheme Programming
For this assignment, you will be writing procedures in Scheme.
Objectives
- learn the basics of functional programming
- write a procedure that creates another procedure
- write recursive procedures to process lists
Specification
You should write the procedures described below. You should name each
procedures as
specified. (I need to know how to call them in order
to test them.) All the procedures should return the specified result.
Create a file called p3.scm which either contains all your
code or loads files containing that code. I should be able to load p3.scm and
then call all your functions. The last line of the file should be a display
statement like
(display "p3 loaded\n")
which will tell me that your file loaded successfully.
- (polarToCartesian distance degrees) Define a function that takes
polar coordinates (a distance from the origin and an angle in degrees) for a
point and returns a list containing the x and y coordinates for the point.
The built-in functions sin and cos need the angle to be converted to radians.
(polarToCartesian 1 45)
should return (.707 .707).
- (filter predicate list) Define a function that takes a predicate
function and a simple list (all elements of a simple list are atoms) and
returns a list containing only those elements for which (predicate element) is
true.
(filter even? '(1 2 3 4 5 6 7 8 9))
should return (2 4 6 8).
Would it ever make sense to filter nested lists in a similar way?
- (count-occurrences symb list) Define a function which takes a
symbol
followed by a list as parameters and returns the number of times that symbol
occurs in the list. The list parameter could have nested lists inside it, you
need to count the occurrences in those as well.
(count-occurrences 'x '(x y x z))
should return 2 and
(count-occurrences 'x '((f x) y ((x z) x)))
should return 3
- (minmax lon) Define a function that takes a list of numbers (you can
assume that the list has only numbers in it) and returns a list containing the
largest and smallest values in the list. You will probably find this is
easier if you use let. It will be worth more points with a let than without.
(minmax '(2 4 6 8 9 7 5 3 1))
should return (1 9)
- (horner-eval coeff x) Define a function which has a
list containing the coefficients of a polynomial (with the coefficient of
x0 first and the coefficient of the highest power of x last)
a0 + a1x + ...
+ an-1xn-1 + anxn
and a value of x, and calculates the value of the polynomial at x using
Horner's rule which restructures the calculation in the following way:
a0 + x(a1 + x( a2 +
... + x(an-1 + x an)...))
For example, you would calculate
1 + 3x + 5x3 + x5
at x=2 by typing
(horner-eval '(1 3 0 5 0 1) 2)
which should return 79.
- In order to handle large amounts of data, Google uses a programming model
that has a map function that generates an intermediate set of data and a
reduce function that somehow combines those results into a final result. This
programming model is relatively easy to spread across multiple processors. (For
more information about map-reduce, see the
Google paper and Joel-on-Software.) In
this exercise, you are going to implement a simplified programming model which
applies a function to each element of a list and then combines these elements
into a final value.
Testing
I will put file called test3.scm which performs minimal
testing of your
implementation and show.scm which test3.scm uses, into the directory
on onyx. Put both files in with your assignment files and load
test3 into Scheme. Your procedures should at least load properly before
running this test. I will do more extensive testing when I grade the
assignment so it is to your advantage to think of as many ways to test your
procedures as possible.
The functions you are defining are all independent of each other so you can
get partial credit even if you don't get them all working. To make testing
easier, any function you don't get working should have a stub with the same
name that returns a message. Put your non-working code in a separate file and
list it's name in your README file so I can look at what you tried.
Submission
Required files
Be sure the file names match exactly. I use a test script to run the
programs; if it doesn't find the files it expects, you will automatically lose
5% of the points for the assignment.
- p3.scm and any files that are loaded by that file.
- README (this file should include anything you want to tell me about this
assignment)
Put all the required files for the assignment in a single directory, change
to that directory and submit using the following command:
submit tcole cs354 3
Hints and Common Problems
- All of these exercises can be done without using assignment. You can use
define and let when it seems appropriate but you should not have to modify the
value of anything you define.
- In Scheme, too many parentheses are just as bad as too few. Unless your
list has a ' in front of it, the interpreter will assume that the first
element is a procedure. So, if you type
((car '(a b c d e)))
you'll get an error message like
procedure application: expected procedure, given: a (no arguments)
- You can put sequences of expressions in your functions, but either don't
surround them by parentheses or put a begin after the opening parentheses to
force them to be evaluated in order.
(lambda (arg1 arg2 ...) expr1 expr2 ...)
If you are looking at things correctly, you will not need this except possibly
for debugging.
- The display function is one that returns no value - if you get error
messages involving trying to use #, the interpreter is probably trying to
use the result of display (or some other expression with an undefined result)
for something else. It usually means you don't have the structure of your expression
quite right.
- If you are using display to help debug your functions, I'd appreciate it
if you take the display stuff out before you submit. I'd rather not see
it. All of these functions should return an appropriate value and do nothing
else.