CSE 341 -- Assignment 6 -- Miranda Warmup

Due in lecture Nov 25, 1998

  1. You don't need to hand in anything for Question 1 -- this is just to give you some practice.

    Experiment a bit with Miranda. Try testing some of the built-in functions. Experiment with Miranda's type system: find the type of some constants, of some built-in functions such as member and map, and of some partially applied functions, e.g. map (const 1). Try to enter some ill-typed expressions, such as [1,2,[3],4] or member [1] "fred".

    Answer the following questions, doing the computation by hand, then checking your answer on the machine.

    Suppose that the following Miranda script has been filed in (it is on orcas on ~borning/miranda/assign6.m).

        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
        my_map2 f [] [] = []
        my_map2 f (a:as) (b:bs) = f a b : my_map2 f as bs
        rev f x y = f y x
        alligator x y = 3+y
    
    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 ::
       my_map2 alligator [1..] [10..]
       my_map2 ::
       my_map2 alligator  ::
       my_map2 (rev alligator)  ::
       alligator (1/0) (6/2)
       alligator (6/2) (1/0)
       alligator ::
       (rev alligator) 10 20
       rev  ::
       rev my_map2::
    

  2. Write and test a Miranda function pythagoras that takes the lengths of the two legs of a right triangle and returns the length of the hypotenuse. For example:
    pythagoras 3.0 4.0  => 5.0
    

  3. Write and test a Miranda function no_duplicates that takes a list of numbers as an argument, and returns a new list which is like the argument but with duplicate elements dropped. The order of the elements in the returned list doesn't matter. For example:
    no_duplicates [1, 3, 3, 8, 5, 3, 5]  => [1, 3, 8, 5]
    no_duplicates [1, 3, 5]  => [1, 3, 5]
    no_duplicates [ ]  => [ ]
    

  4. Write and test a Miranda function deriv that takes a function f as an argument and returns a function that is (approximately) its derivative f'. Thus the type of deriv should be
    (num -> num) -> (num -> num)
    
    For example
    deriv sin
    
    should return a function. If you apply this function to 0, it should return (approximately) 1, since the derivative of sin is cos, and cos 0 is 1. As another example, suppose we define
    double x = 2*x
    
    Then deriv double should return a function. If you apply this function to 100, it should return (approximately) 2.

Hints: for Problems 2 and 3 it is OK to look at the sample solutions for previous assignments for this quarter. Problem 4 is not as hard as it might appear at first glance. Your function definition can look like
deriv f x = ...
since currying will give the correct type for this. Can you compute the derivative symbolically, or do you need to approximate it numerically? If you approximate it numerically, you can define a constant epsilon for use in the approximation:
epsilon = 1.0e-8
(You don't need to do anything fancy about numerical accuracy.)