CIS 554 Haskell 1: Exercises
Fall 2013, David Matuszek
In the Haskell REPL, :t thing
will tell you the type of thing
. Here are some examples:
Type  Interpretation 

not :: Bool > Bool 
not is a function that, given a boolean value, returns a boolean value. 
head :: [a] > a 
head takes a list of some arbitrary type a , and returns a value of type a . 

mod takes two values of type a , which must be an Integral type, and returns a value of type a . 
map :: (a > b) > [a] > [b] 
map takes a function from a to b , and a list of a , and returns a list of b . 
Write the following functions. Where builtin equivalents exist, do not define a function in terms of the equivalent builtin function. For example, don't say
. (You can use elem
in other definitions, though.)
The following functions can all be defined in a single line, so try to do so. They do not need to work for infinite lists.
member :: Eq a => a > [a] > Bool
count :: (Num a, Eq a1) => a1 > [a1] > a
forall :: (a > Bool) > [a] > Bool
exists :: (a > Bool) > [a] > Boo
lsingle :: (a > Bool) > [a] > Bool
mostly :: (a > Bool) > [a] > Bool
majority :: [Bool] > Bool
collatz1 :: Int > Int
collatz :: Int > Int
isFixpoint :: (Eq a) => (a > a) > a > Bool
f
is a value x
such that f x = x
. This function should return True if its second argument is a fixpoint of its first argument. findFixpoint :: (Eq a) => (a > a) > a > Int > Maybe a
f(f(f(...x...)))
) will yield a fixpoint. Integer divide by 2 is another such function. findFixpoint f x limit
will try to find a fixpoint of f
, starting with x
. If it succeeds, it will return Just y
, where y
is the fixpoint. If it fails to find a fixpoint after limit
applications of f
, it will return Nothing
.tooBig :: Float > Bool
nearlyEqual :: Float > Float > Bool
series :: (Int > Float) > Int > [Float]
f
and an initial value i
, return the infinite series [f(i), f(i+1), f(i+2), f(i+3), ...]
. seriesSum :: (Int > Float) > Int > [Float]
f
and an initial value i
, return the infinite series [f(i), f(i)+f(i+1), f(i)+f(i+1)+f(i+2), f(i)+f(i+1)+f(i+2)+f(i+3), ...]
.seriesSum
to compute the values of each of the listed convergent series. You can stop the summation when the terms become smaller than 0.00001. Name your methods computeLn2
, computePiOver4
, computeTwo
, computeE
, computePiSquaredOver6
, computeTwoByPowers
, computeTwoThirds
, and computePhi
. For many of these you will need to write additional functions.Provide tests for the above functions. Haskell does not come with a testing framework, and I would rather not ask you to download and install one, so here's a very small framework for you to use.
assertTrue :: Bool > String > IO ()
assertTrue x claim = if x
then putStrLn claim
else putStrLn $ "*** Untrue: " ++ claim
assertFalse :: Bool > String > IO ()
assertFalse x claim = assertTrue (not x) claim
assertEqual :: (Eq a, Show a) => a > a > IO ()
assertEqual x y = if x == y
then putStrLn $ (show x) ++ " equals " ++ (show y)
else putStrLn $ "*** " ++ (show x) ++ " does not equal " ++ (show y)
And here is a brief example of its use:main = do
assertTrue (member 3 [1, 2, 3, 4]) "3 is in the list [1, 2, 3, 4]"
assertFalse (member 7 [1, 2, 3, 4]) "7 is not in the list [1, 2, 3, 4]"
assertEqual 3 (count 'a' "abracadabra")
Your program is due before 6am Wednesday, October 16. Submit via Canvas.