In the Haskell REPL,
:t thing will tell you the type of
thing. Here are some examples:
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] -> Bool
count :: (Num a, Eq a1) => a1 -> [a1] -> a
forall :: (a -> Bool) -> [a] -> Bool
exists :: (a -> Bool) -> [a] -> Bool
single :: (a -> Bool) -> [a] -> Bool
mostly :: (a -> Bool) -> [a] -> Bool
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] -> Bool
equalSet :: Eq a => [a] -> [a] -> Bool
setDiff :: 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.
And here is a brief example of its 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)
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")
There are various measures of "readability"--how good a reader need to be in order to understand a passage of English text. These measures are based on the average length of words (usually measured in syllables) and the average length of sentences (measured in words). The result is usually given as the number of years a child has to have attended school (her grade level) in order to understand the text. These measures are crude, but better than nothing. Your assignment is to let the user read in a passage of text (from a file), apply the formulae, and print out the results.
Your program should read in a sample of text from a file, and use a variation of the Lix index to determine the readability of the text. It basically counts long sentences and big words. (I picked this one because it doesn't require counting syllables!) The formula to apply is:
words/sentences + 100 * (words >= 6 characters)/words
') is considered to be a letter.
: . ! ?) appears.
The Haskell function
words may be helpful. (There is also an
unwords, which you should know about but probably won't need.) However, you will still have to remove punctuation marks from the ends of words in order to count how long they are.
Finally, don't get hung up on the details. The Lix measure isn't rocket science, and I won't care if your results are off by a few percent.
Your program is due before 6am Wednesday, October 17. Submit via Blackboard.