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 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. (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] > Bool
count :: (Num a, Eq a1) => a1 > [a1] > a
forall :: (a > Bool) > [a] > Bool
exists :: (a > Bool) > [a] > Bool
first :: (a > Bool) > [a] > Maybe a
single :: (a > Bool) > [a] > Bool
mostly :: (a > Bool) > [a] > Bool
mostlyTrue :: [Bool] > Bool
majority :: Eq a => [[a] > Maybe a
Nothing
if
there is no majority element. See http://www.cs.utexas.edu/~moore/bestideas/mjrty/
for the clever twopass algorithm to do this.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), ...]
.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.