[Haskell-beginners] example of compostion
Rainer Grimm
rainer at grimm-jaud.de
Mon Jan 31 22:15:18 CET 2011
Hello,
I'm looking for a good example of function composition. It's very
impressive, how concise you can extract information out of a file in
Haskell. So I write a short programm, which gets all title words out of
a given file sorted by their length.
How could the following programm be improved?
import Data.Char (isUpper,isLower)
import Data.List (sortBy)
import Data.Ord (comparing)
isTitle word=
isFirstUpperCase ( word) && isTailLowerCase ( word )
where isFirstUpperCase= isUpper . head
isTailLowerCase= all isLower . tail
main = do
putStrLn "The name of the input file:"
file <- getLine
inputString <- readFile file
let allTitles= reverse . sortBy (comparing length ) . filter isTitle $
words inputString
putStrLn $ show $ take 15 allTitles
putStrLn $ show (length allTitles) ++ " titles in " ++ file
Thanks in advance,
Rainer
--
Rainer Grimm rainer at grimm-jaud.de
http://www.grimm-jaud.de
<javascript:void(0);>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110131/e3df3b8d/attachment.htm>
More information about the Beginners
mailing list