[Haskell-beginners] Reading Multiple Files and Iterate Function Application

Daniel Fischer daniel.is.fischer at web.de
Mon Oct 11 13:38:44 EDT 2010


On Monday 11 October 2010 18:06:50, Lorenzo Isella wrote:
> Thanks a lot Daniel, but I am a bit lost (up to not long ago I did not
> even know the existence of a control monad...and some unstructured
> reading did not help).

I think there's a misunderstanding here. Control is the top-level name for 
stuff related to control flow, like

Control.Concurrent

for concurrency combinators,

Control.Parallel (and Control.Parallel.Strategies)

for parallelism combinators. Monads (some) are also related to control 
flow, so the monad stuff that's not available from the Prelude lives in

Control.Monad and Control.Monad.Whatever (Control.Monad.State, 
Control.Monad.Writer, ...)

Control.Monad (part of the standard libraries) provides a lot of general 
functions for working with Monads, among them forM (which is "flip mapM").

> Some online research about mapM and fmap led me here
> http://en.wikibooks.org/wiki/Haskell/Category_theory
> and I think I am a bit astray at this point ;-)
>
> Why does my "simple" snippet below raise a number of errors?
> Cheers
>
> Lorenzo
>
>
> import Data.Ord
>
> import Data.List
>
> main :: IO ()
>
> main = do
>
>    let nums=[1,2]
>
>    let fl = getAllLengths nums

That means fl is the IO-action which gets the file lengths. You want the 
result, thus

    fl <- getAllLengths nums

to bind the result of that action to the name fl.

>
>    putStrLn "fl is, "
>    print fl
>
>
> filename :: Int -> FilePath
> filename i = "file" ++ show i ++ ".dat"
>
> fileLength :: FilePath -> IO Int
> fileLength file = fmap length (readFile file)
>
> getAllLengths :: [Int] -> IO [Int]
> getAllLengths nums = mapM (fileLength . filename) nums



More information about the Beginners mailing list