Simon Raahauge DeSantis wrote:
> [...] Here's the snippet in particular (and the results from
> evaluation in hbi):
> > getDirectoryContents "." >>= filter (\(x:_) -> x /= '.');
> [65] Cannot unify types:
>     Prelude.IO
> and (Prelude.[])
>  in  (>>=) (getDirectoryContents ".") (filter (\I ->
> case I of {
>   Prelude.[] -> Pfail "No match in Pinteractive"
> | (:) x I4625 -> (/=) x '.'
> }
> ))
> [...] Is this because the function being used with filter would
> break when given an empty list (or is that Pfail bit there to
> handle that breaking?)

The reason is that getDirectoryContents is an IO *action*,
filter (\...) is a *function*, and the >>= operator sequences two
actions, as can be seen from their types:

   getDirectoryContents          ::  FilePath  -> IO [FilePath]
   filter (\(x : _) -> x /= '.') :: [FilePath] ->    [FilePath]
   (>>=)                         :: Monad a => a b -> (b -> a c) -> a c

So you have to lift your filtering into the IO monad:

   getDirectoryContents "." >>= (\xs -> return (filter (\(x:_) -> x /= '.') xs))

or to impress your friends with your higher-order programming skills:
;-)

   liftM (filter $ (/= '.') . head) $ getDirectoryContents "."

The funny Pfail in the error message stems from the fact that hbc
typechecks a desugared version of the source program, which can be
confusing.

Hope that helps,
   Sven
-- 
Sven Panne                                        Tel.: +49/89/2178-2235
LMU, Institut fuer Informatik                     FAX : +49/89/2178-2211
LFE Programmier- und Modellierungssprachen              Oettingenstr. 67
mailto:[EMAIL PROTECTED]            D-80538 Muenchen
http://www.pms.informatik.uni-muenchen.de/mitarbeiter/panne



Reply via email to