module HW1 where import Prelude hiding (filter) {- For each of the following questions, put your answer directly below the question. This homework is due on Friday, September 11, by 3pm. Email your code directly to Adam Procter: amp269@mizzou.edu. (1) You must use a text editor (e.g., vi, textpad, emacs, etc.) to prepare your solution. (2) You must write type declarations for each and every one of your Haskell definitions. (3) The program you turn in must be the product of your effort alone. -} {- Define functions for arithmetic on complex numbers (e.g., usually written: a + bi). You must represent complex numbers in Haskell as pairs of Float (i.e., (a,b)). If you haven't heard of complex numbers, check out the wikipedia entry: http://en.wikipedia.org/wiki/Complex_number -} -- 1. Define a function, magn, that computes the magnitude of a complex -- number. Recall that the magnitude of a complex number, a+bi, is defined -- as: squareroot (a^2 + b^2). You can use the built-in Haskell function, sqrt, -- in your answer. Put your definition below: -- 2. Define a function, add, that takes two complex numbers and adds them. -- Recall that (a + bi) + (c + di) = (a+c) + (b+d)i. -- 3. Define a function, sub, that subtracts two complex numbers. -- Recall that (a + bi) - (c + di) = (a-c) + (b-d)i. -- 4. Define a function, mult, that multiplies two complex numbers. -- Recall that (a + bi) * (c + di) = (a*c - b*d) + (a*d + b*c)i -- 5. Here is how a function called filter is defined in the Haskell -- standard prelude: filter :: (a -> Bool) -> [a] -> [a] filter p [] = [] filter p (x:xs) = if p x then x : filter p xs else filter p xs -- (filter p l) collects together the list elements of l on which p is true. -- Ex. Hugs> filter odd [1,2,3,4,5] -- [1,3,5] -- -- Write a function, sqrodd, that takes a list of Ints and returns the same -- list except that any odd number is squared. -- Ex. Hugs> sqrodd [1,2,3,4,5] -- [1,2,9,4,25] -- You must write the function from "scratch" --- i.e., you can't use any -- other built-in Haskell function except odd. Hint: the code for sqrodd -- will look eerily similar to filter.