ASSIGNMENT 1 ------------ Submission Deadline: 11th February - Please start early or you will run into problems. 1. Given a list xs = [x_1, x_2, x_3,..., x_n] of numbers, the sequence of successive maxima, ssm xs, is the longest subsequence [x_j1, x_j2,...,x_jm] such that j1 = 1 and x_ji < x_jk for ji < jk. For example, ssm [3,1,3,4,9,2,10,7] = [3,4,9,10]. Express ssm in terms of foldl. 2. Define a binary tree as: data Bintree a = Leaf a | Bnode (Bintree a ) (Bintree a) deriving Show and a general tree as: data Gentree a = Gnode a [Gentree a] Now consider the expression f(g(a,b),h(c),d). Considering each of the function and variable symbols to be a Char, represent the above expression as a Gentree Char. Write the curried version of the same expression. Represent the curried version as a Bintree Char. (Hint: In the representation of the curried version, the constructor Bnode should be interpreted as an application node.) Now define the following two function: curry :: Gentree Char -> Bintree Char converts an expression from uncurried to curried form. uncurry :: Bintree Char -> Gentree Char converts the expression from curried to uncurried form. 3. a. The mathematical definition of the derivative f' of the function f is: f'(x) = lim h -> 0 (f (x + h) - f (x))/ h On the basis of this, write a function diff such that diff :: (Float -> Float) -> (Float -> Float) such that diff f = f' b. The zero of a function f is a value x such that f(x) = 0. To find the zero of f, proceed as follows. Let b an approximation for the zero of f. Then a better approximation is given by b - f(b)/f'(b). Write a function zero f which will give the zero of a function f. c. Use zero to: (i) write the function cube_root with the obvious meaning. (ii) define a function find_years such that (find_years calc p r f) which will calculate the number of years that money has to be left in a bank so that starting with the principal amount p and the interest rate r, the final amount will be f. To make it more general, find_years is calculated in terms of a formula c which gives the interest accrued from the principal, the rate of interest and the time for which money is kept in the bank. 4. Consider the representation of a geometric region as having the type: type Region = Point -> Bool Point is given by the type (Float, Float). The idea is that the only use that we shall put a region to is to ask whether a point belongs to it or not. a. First define a function circle_maker which will take a radius and produce a circular region around the origin with the given radius. circle_maker :: Float -> Region b. Similarly define a rectangle_maker which will take a length and a breadth and produce a rectangle around the origin. rectangle_maker :: Float -> Float -> Region c. Define the regions not_in, intersection, union, annulus with the types not_in :: Region -> Region intersection :: Region -> Region -> Region union :: Region -> Region -> Region annulus :: Region -> Region -> Region d. Finally define a function called translate which will translate a region to a given distance: translate :: Region -> Point -> Region 5. Consider the representation of small language in Haskell: A program is a list of function definition followed by a single expression type Program = ([Fundecl], Exp) data Fundecl = Fun Funname Var Exp -- single argument only data Funname = F | G | H -- add more names if you want data Var = X | Y | Z -- add more variables if you want data Exp = Con Int | Var Var | Add Exp Exp | Funapp Funname Exp -- It is permissible for a type and a data constructor to have the same name. Assume that the language is eager. Write an interpreter eval for the language: eval :: Program -> Int