[Haskell-beginners] yet another monad question

David McBride toad3k at gmail.com
Sat Feb 4 12:05:55 CET 2012


When you pass an argument to
readDir = findRegularFiles >>= readFiles

it expands to
readDir arg = (findRegularFiles >>= readFiles) arg

which fails because that expression takes no argument, only
findRegularFiles does.  Honestly I can't think of any way to get that
argument in there without explicitly naming it.

And no, you cannot go like this either:
readDir = readFiles =<< findRegularFiles

It still has the same problem.

On Sat, Feb 4, 2012 at 5:49 AM, Ovidiu Deac <ovidiudeac at gmail.com> wrote:
> I have the following code which works fine:
>
> type File = (String, String) --name and content
>
> readDir :: String -> IO [File]
> readDir dir = findRegularFiles dir >>= readFiles
>   where
>     findRegularFiles = find always (fileType ==? RegularFile)
>     readFiles paths = mapM readFile paths >>= return.zip paths
>
> ...and I would like to write the function readDir like this:
> readDir :: String -> IO [File]
> readDir = findRegularFiles >>= readFiles
>   where ...
>
> ...but I get the error:
> grep.hs:46:32:
>     Couldn't match expected type `IO [FilePath]'
>                 with actual type `[FilePath]'
>     Expected type: IO [FilePath] -> String -> IO [File]
>       Actual type: [FilePath] -> IO [File]
>     In the second argument of `(>>=)', namely `readFiles'
>     In the expression: findRegularFiles >>= readFiles
>
> Can somebody please explain it?
>
> It's not a big deal. I can keep the old version which works fine. My only
> problem is that I thought I understood the monads better but apparently I
> don't :)
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



More information about the Beginners mailing list