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

Lauri Alanko la at iki.fi
Sun May 25 18:29:31 EDT 2008


On Tue, May 20, 2008 at 09:15:57AM +0100, Yann Golanski wrote:
> 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)

Note that there is a standard function "lines" which splits a string
into lines.

> Nice, simple and I understand what it is doing.  
> 
> 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.
> 
> Any pointers as to how to do that?

import System.Random

removeRandomElement :: [a] -> IO (a, [a])
removeRandomElement l = do i <- randomRIO (0, length l - 1)
                           return (removeAt i l)

where removeAt is from problem 20 above.

And you use it like anything else in the IO monad:

do ...
   images <- parseImageFile ...
   ...
   (chosen, rest) <- removeRandomElement images
   ...

> 3- Wait and do something later.... How, I have no idea how to do that!
> Help?

Control.Concurrent.threadDelay is the simplest way to make a thread
sleep for a while. However, if you're using some GUI library, you may
want to use the library's own timers.

> 4- I guess that progress bars and updating text will be somewhere in the
> GUI (I chose wxHaskell)...  Again, no idea where.

I'm not familiar with wxHaskell, sorry.

> 5- How do you call an external program in Haskell?  Either xv or
> Esetroot will do the job of displaying the image.  Is there a Haskell
> native way to do that?

There is a direct X11 library for Haskell, but you're probably better
off just calling an external program. See the System.Process module.

Hope this helps.


Lauri


More information about the Haskell-Cafe mailing list