[Haskell-beginners] yet another monad question

Markus Läll markus.l2ll at gmail.com
Sat Feb 4 13:49:55 CET 2012


It's not about monads, that this doesn't work. To go point-free you
need to understand the types of the expressions you compose.

The bind function ((>>=) :: Monad m => m a -> (a -> m b) -> m b)
doesn't leave room to take more arguments. Matching the arguments to
bind in the expression 'findRegularFiles >>= readFiles' the
'findRegularFiles' should have a type of 'Monad m => m a' and
'readFiles' 'Monad m => a -> m b'. With this type 'findRegularFiles'
takes no arguments, but it should take one -- the directory-path. But
the 'readFiles' type looks to be right: bind expects a function from
'a' to 'm b', and that's what it gets. So, what you need is some other
function than bind (>>=) to compose the functions you have to get the
whole expression to have the right type. If you find the type of that
function, you'll easily find the composing function itself.

Also check the definitions of 'findRegularFiles' and 'readFiles', but
at first you can leave them be
... where
  findRegularFiles :: String -> IO [String]
  findRegularFiles dir = undefined
  readFiles :: [String] -> IO [File]
  readFiles paths = undefined
and implement them later.


On Sat, Feb 4, 2012 at 12:49 PM, 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
>



-- 
Markus Läll



More information about the Beginners mailing list