Due in lecture Nov 25, 1998
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+yWhat 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::
pythagoras 3.0 4.0 => 5.0
no_duplicates [1, 3, 3, 8, 5, 3, 5] => [1, 3, 8, 5] no_duplicates [1, 3, 5] => [1, 3, 5] no_duplicates [ ] => [ ]
(num -> num) -> (num -> num)For example
deriv sinshould 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*xThen deriv double should return a function. If you apply this function to 100, it should return (approximately) 2.
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.)