[Haskell-cafe] Rotating backdrop (aka learning Haskell)

Derek Elkins derek.a.elkins at gmail.com
Tue May 20 12:45:57 EDT 2008


On Tue, 2008-05-20 at 10:55 +0200, Ketil Malde wrote:
> Yann Golanski <yann at kierun.org> writes:
> 
> > 1- Get a list out of a file:  I managed to do that using the following:
> >
> > parseImageFile :: FilePath -> IO [String]
> > parseImageFile file = do inpStr <- readFile file
> >                          return $ filter (/="") (breaks (=='\n') inpStr)
> >
> > Nice, simple and I understand what it is doing.  
> 
> Can be improved:
> 
>   breaks (=='\n') == lines              -- easier to read, no?
>   filter (/="") == filter (not . null)  -- more polymorphic, not important here
>   do x <- expr1        ==  expr1 >>= return . expr2 
>      return $ expr2 x      -- i.e. "readFile f >>= return . filter (not.null) . lines"

do x <- expr1; return $ expr2 x 
== expr1 >>= return . expr2
== liftM expr2 expr1 -- or fmap (a.k.a. <$>) if you like

So,
liftM (filter (not . null) . lines) readFile
alternatively,
filter (not . null) . lines <$> readFile



More information about the Haskell-Cafe mailing list