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, preceding each function with the given
type declaration. Where built-in equivalents exist, do not define a
function in terms of the equivalent built-in 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. (Hint:
Remember that map results in a list of the same
length, filter results in a potentially shorter list, and foldl
results in a single value.)
member :: Eq a => a -> [a] -> Boolcount :: (Num a, Eq a1) => a1 -> [a1] -> aforall :: (a -> Bool) -> [a] -> Boolexists :: (a -> Bool) -> [a] -> Boolfirst :: (a -> Bool) -> [a] -> Maybe asingle :: (a -> Bool) -> [a] -> Boolmostly :: (a -> Bool) -> [a] -> BoolmostlyTrue :: [Bool] -> Boolmajority :: Eq a => [[a] -> Maybe aNothing if
there is no majority element. See http://www.cs.utexas.edu/~moore/best-ideas/mjrty/
for the clever two-pass algorithm to do this.collatz1 :: Int -> Int collatz :: Int -> IntisFixpoint :: (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 -> BoolnearlyEqual :: Float -> Float -> Boolseries :: (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), ...].computeLn2,, computeTwo,
computeE, computePiSquaredOver6, computeTwoByPowers,
computeTwoThirds, and computePhi
computePsi. You can use seriesSum, and for
some of these you may need to write additional functions. *Main> let fibonacci = 1 : 1 : (zipWith
(+) fibonacci (tail fibonacci))
*Main> take 10 fibonacci
[1,1,2,3,5,8,13,21,34,55]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. Tests are required for all the assigned functions.
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 Monday, October 26. Submit via Canvas.