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

Ketil Malde ketil at malde.org
Tue May 20 04:55:39 EDT 2008

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"

> 2- Get a random element from a list and remove it:  Okay, this I
> understand less well.  I looked at the solutions of problems 23 and 20
> in http://www.haskell.org/haskellwiki/99_questions so there is a
> skeleton there.   However, my list is IO [String].... Hum, monads.

Since you really want to shuffle the list, how about assigning each
entry a random number, and sorting the list?

  shuffle :: [String] -> IO [String]
  shuffle fs = do 
          rs <- randoms
          return (map snd $ sort $ zipWith rs fs)

> 5- How do you call an external program in Haskell?

See the 'System.Process' or 'System.Cmd' modules.

If I haven't seen further, it is by standing in the footprints of giants

More information about the Haskell-Cafe mailing list