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 :: Integral a => a -> a -> 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 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.
member :: Eq a => a -> [a] -> Boolcount :: (Num a, Eq a1) => a1 -> [a1] -> aforall :: (a -> Bool) -> [a] -> Boolexists :: (a -> Bool) -> [a] -> Boolsingle :: (a -> Bool) -> [a] -> Boolmostly :: (a -> Bool) -> [a] -> BoolisSet and makeSet,
you may assume that the lists are "sets," that is, contain no duplicate
elements.
isSet :: Eq a => [a] -> Bool makeSet :: Eq a => [a] -> [a]subset :: Eq a => [a] -> [a] -> BoolequalSet :: Eq a => [a] -> [a] -> BoolsetDiff :: Eq a => [a] -> [a] -> [a]setUnion :: Eq a => [a] -> [a] -> [a]setIntersection :: Eq a => [a] -> [a] -> [a]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")
['a', 'e', 'i',
'o', 'u'], add "idig" to the beginning of the
word. Example: "art" → "idigart"."idig" after each consonant cluster in the word,
except a consonant cluster that ends the word. Examples: "rabbit"
→ "ridigabbidigit", "schedule"
→ "schidigedidigulidige".There are two other "clean up" rules:
"Sam" → "Sidigam",
"Alice" → "Idigalidigicidige".The Haskell functions words and unwords may
be helpful.
Your program is due to Canvas before 11:59pm Tuesday, October 25.