[ ^ index   | ^ all homeworks ]

CSE 341 : Homework 2

Due: April 9, 2001, at the beginning of class

Directions: Submit a stapled hard copy with your name(s), section(s), and the answers to each of the questions below. You should type in code and English text answers. You may draw pictures by hand.


1. For the following declarations, draw the box-and-arrow diagrams that result, starting from all source variables.
a.
val a = [3, 4, 5];
val b = [a, a];
val c = b :: b :: nil;
b.
val i = "hi" :: ["bob"];
val j = (i, tl(i), hd(i) ^ "there", 3.4, i);

2. For each of the following functions, explain what the type is that is inferred (if the function declaration is legal), or explain what  type error results (if illegal), without using the ML interpreter. Give an example of calling it on legal inputs, and show the type of the result of the call. Explain your reasoning.
a.  fun f1(x) = x
b.  fun f2(a, b) = {bob = b+0.5, joe = "steve " ^ a}
c.  fun f3(a, b, c) = [a, b] :: c

3. Implement a function quad_roots that returns a pair of roots of a quadratic polynomial. I.e., given real numbers a, b, and c, compute and return the two values (-b +/- sqrt(b^2 - 4ac)) / 2a. Use let expressions and local value bindings to avoid any repeated calculations.

4. Using lists to represent stacks, implement functions push (which takes a stack and an element and returns a new stack with the element added to the top of the stack), pop (which takes a stack of at least one element and returns the stack without the first element), top (which takes a stack of at least one element and returns the first element), and isEmpty (which takes a stack and returns a boolean indicating whether or not the stack is empty). Also define the value emptyStack which is bound to an empty stack. Your functions and value should be as polymorphic as possible.  Give the types of your functions and value along with their definitions.

5. Implement the function sigma(m:int, n:int) which computes the sum of all integers from m to n, inclusive. Use recursion.


Craig Chambers

Last modified: Mon Apr 2 17:14:27 PDT 2001