|| Solutions to CSE 341 Miranda Discussion Questions || Write a Miranda function to find the cube of a number. cube :: num->num cube n = n^3 || alternate version: cub n = n*n*n || Write a Miranda function to find the sum of three numbers. || remember that -> is right associative, so this type could also be written || sum3 :: num -> (num -> (num->num)) sum3 x y z = x+y+z || Write a Miranda function to find the sum of a list of numbers. ||There is already a built-in function sum, so let's call this one || mysum. We make the sum of the empty list 0 (the additive identity). mysum :: [num]->num mysum [] = 0 mysum (n:ns) = n + mysum ns || Write a Miranda function to find the maximum of two numbers. || || Call it my_max2 to avoid conflict with the built-in one: my_max2 :: num->num->num my_max2 x y = x, if x>=y = y, otherwise || Write a Miranda function to find the value of the quadratic || expression ax^2 + bx + c for parameters a, b, c, and x. quad_expr :: num->num->num->num->num quad_expr a b c x = a*x^2 + b*x + c || Write a Miranda function to find the two roots of the quadratic || equation ax^2 + bx + c = 0 for parameters a, b, and c. quad_roots :: num->num->num->(num,num) quad_roots a b c = ( (-b+r)/(2*a) , (-b-r)/(2*a) ) where r = sqrt (b*b - 4*a*c) || Write a Miranda function to reverse a list. my_rev [] = [] my_rev (x:xs) = my_rev xs ++ [x] || Write a function my_map2 that is analogous to map but works || for functions of two arguments rather than one. my_map2 :: (*->**->***) -> [*] -> [**] -> [***] my_map2 f [] [] = [] my_map2 f (x:xs) (y:ys) = f x y : my_map2 f xs ys || Tacky true/false questions! || In Miranda, programs would give the same answers if we replaced lazy || evaluation with call-by-name. || || True. (We might do unnecessary compuation, but we'd never evaluate an || expression using call-by-name that was not evaluated using lazy evaluation, || or vice versa.) || In Miranda, programs would give the same answers if we replaced lazy || evaluation with call-by-value. || || False. With call-by-value, we might evaluate an expression whose value is || never used. || Write a Miranda function to return the infinite list of amounts of || money you have every year, assuming you start with $1 and get paid 5% || interest, compounded yearly. dollars = dollar_growth 1.0 0.05 dollar_growth p rate = p : dollar_growth (p*(1+rate)) rate || Suppose that the following Miranda script has been filed in ... plus x y = x+y append [] ys = ys append (x:xs) ys = x : append xs ys || my_map2 f [] [] = [] || my_map2 f (x:xs) (y:ys) = f x y : my_map2 f xs ys || What is the result of evaluating the following Miranda expressions? || Try them and see ...