Computer Science & Engineering 505
Assignment 7

Due: Dec 8, 1999

This is the final assignment for the quarter, and intended to be easy (since it's the end of the quarter and you also have projects due).

  1. Write and test a recursive my_map2 function in Miranda. This function should take another function as an argument, apply it to corresponding elements of the following two lists, and return a list of the results. For example,
    my_map2 (+) [1,2,3] [10,11,12]
    
    should evaluate to [11,13,15]. It is an error if the lists are of differing lengths. (Use the built-in function error in handling this.) There is a built-in map2 function in the Miranda standard library -- please write your own though.

  2. What is the type of my_map2?

  3. Write and test a function smember that takes a sorted list of numbers, then a number, and returns true or false depending on whether the number is an element of the list. Since it works on sorted lists, it should handle infinite lists. For example:
    smember [1,2,3] 3  returns True
    smember [10..] 5  returns False
    smember primes 11  returns True
    smember primes 10  returns False
    
    where primes is the infinite list of primes (defined elsewhere).

  4. Suppose that the following Miranda script has been filed in.
        cube x = x*x*x
        my_map f [] = []
        my_map f (x:xs) = f x : my_map f xs
        my_append [] x = x
        my_append (x:xs) ys = x : my_append xs ys
    
    What is the result of evaluating the following Miranda expressions? (If there is a compile-time type error, or a run-time error, or a non-terminating computation, say so.) If the expression is followed by :: then give the type, instead of the value.
       cube 3
       cube ::
       my_map ::
       my_map cube ::
       my_map cube [1..]
       my_append ::
    
    (You can check your answers by trying them with Miranda, but I recommend trying to figure it out by hand first, since there will be a similar question on the final.)