CS 1MD3 - Winter 2006 - Assignment #3

Generated for Student ID: 0141177

Due Date: Monday, February 27th

(You are responsible for verifying that your student number is correct!)

The following exercises involve writing very short (but tricky) functions to manipulate data structures. One of them is a recursive function which may be written in only three lines, while the rest may be written in only one line each (and are not recursive). Function signatures (the "def functionName(x,y,z):" lines) are not counted when enumerating the number of lines. So in this context, a "one-line function" actually occupies two lines: the signature on the first line, and the body on the next line.

Note: Not all students get the same number of questions because some questions are more difficult than others. We have tried to make all the assignments of roughly equal difficulty.

Question 1:
Implement the listLength function (same as len() for lists) as a recursive function--obviously
without using len() itself.

Question 2:
A (binary) "symmetric relation," R, is a set of ordered pairs, (x,y), such that 
if (x,y) is in R, then so is (y,x).  For example, the relation
R1 = {(1,2), (3,4), (2,1), (5,5)} is not symmetric, while the relation
R2 = {(1,2), (3,4), (2,1), (5,5), (4,3)} is symmetric.
The "symmetric closure" of a relation, R, is the smallest symmetric relation
which contains R as a subset.  In other words, it's what you get after you add
any "missing" symmetric pairs to the relation.  R2 is the symmetric closure of R1
in the examples above.
Write a one-line function which accepts a relation (a set of 2-element tuples), and
returns its symmetric closure.

Question 3:
The dot product of two vectors x = (x1, x2, ..., xn) and y = (y1, y2, ..., yn) is the
sum: x1*y1 + x2*y2 + ... + xn*yn.
Implement the dotProduct function on one line using reduce, zip, a list comprehension, 
and a lambda form.
Your function should accept two parameters (either lists or tuples) and return their dot product.