11.  Pattern Matching in Symbolic Programming:  Homework

Last revision:  February 18 1998

Author: YourNameGoesHere

In solving problems from this point forward, incorporate the aspects of programming style covered in this section.

1: multiply-defined f

Write a multiply defined function "f[]" such that 
(a)  f[ "dog" ] = "cat"
(b)  f[ 4 ] = 7
(c)  f[ Pi ] = E
(d)  f of an Integer other than 4 is equal to 29
(e)  f of anything else is equal to the square of the value of the parameter.

2: triangle

Define a function "triangle[x]" to be equal to (1-Abs[x]) when Abs[x]<1 and 0 otherwise.  Plot your triangle function.

3: sgn

Write a definition for the function "sgn[x]", which is equal to 1 if x is positive, -1 if x is negative, and 0 if x is zero.  Plot "sgn[x]".

4: doubleY

(a) Using a structured pattern, write a function "doubleY[]"  that takes a pair {x,y} and returns {x,2y}.

(b) Map "doubleY[]" onto the list {{1,1},{2,2}}.

5: {x,y,z} ↦ {x+y,z}

Write a definition, using a structured pattern, for a function which replaces the triple {x,y,z} with the pair {x+y,z}.

6: enoughToAverage

Averages are important only when there are enough data points.  Write a function "enoughToAverage[]" which will give the average value of a list of five or more things.  If the list has fewer than five elements, the function should be undefined.  For instance:

enoughToAverage[{1,2,3,4,5}] should return 3.
enoughToAverage[{1,2,3}]  should return enoughToAverage[{1,2,3}].

Hint:  Use a pattern that checks if the input has length greater than or equal to five.