Thursday, April 23, 2009

Solutions to Chapter 1 (p. 16)

Excercises 1 and 2 are mere ghci interaction assignments.

#3. The original WC program counts lines, and looks like this (p.15):

main = interact wordCount
       where wordCount input = show (length (lines input)) ++ "\n"

The key part is length (lines input)), which applies the length function to the result of lines input. The excercise tells us about the words function, which makes the solution trivial:

main = interact wordCount
       where wordCount input = show (length (words input)) ++ "\n"

#4. For the number of characters, we just need the length of the original input string, thus:

main = interact wordCount
       where wordCount input = show (length input) ++ "\n"

No comments:

Post a Comment