[Haskell] Probably a trivial thing for people knowing Haskell

Udo Stenzel u.stenzel at web.de
Mon Oct 20 18:49:45 EDT 2008


Friedrich wrote:
> Taral <taralx at gmail.com> writes:
> > Wow, talk about doing everything by hand. :) There are a lot of
> > utility functions that make your life easier. Try this:

Given a strict pair, it should work:

> > import Control.Monad
> > import Data.Char
> > import Data.List
> > import System.Directory
> > import System.IO
> > import Text.Regex
> >

data Pair = Pair !Integer !Integer

> > main = do
> >     allFiles <- getDirectoryContents "."
> >     let files = filter (isDigit . head) allFiles
> >     contents <- mapM readFile files
> >     let (sum, count) = foldl' countDownloads (0,0) $ lines $ concat contents

let Pair sum count = foldl' countDownloads (Pair 0 0) $ lines $ concat contents

> >     putStr ("Download = " ++ show sum ++ " in " ++ show count ++ " days are " ++
> >  show (fromIntegral sum / fromIntegral count) ++ " downloads/day\n")
> >
> > match = matchRegex $ mkRegex "([0-9]+) Windows ex"
> >
> > countDownloads (s, c) l =
> >     case match l of
> >         Just [n] -> (s + read n, c + 1)
> >         Nothing -> (s, c)

countDownloads p@(Pair s c) l =
    case match l of
        Just [n] -> Pair (s + read n) (c + 1)
        Nothing -> p



-Udo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell/attachments/20081021/b87e1ec0/attachment.bin


More information about the Haskell mailing list