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

Ryan Ingram ryani.spam at gmail.com
Tue May 20 13:31:25 EDT 2008

Rather than golfing your questions (which seems popular), I'll try to
give you some concrete answers.

First off, I agree with Bulat that this is probably too large of a
first project; doing it in pieces seems like it would be a lot
simpler.  In particular "if the file changes, ..." isn't a requirement
I'd put on the project for a "first attempt".  And just getting the
non-GUI version to work is a good start; then you can go about
learning a GUI library, which can be a project all by itself :)

2008/5/20 Yann Golanski <yann at kierun.org>:
> 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.

There's nothing tricky about that, though (although 'and remove it' is
a bit odd from a Haskeller's point of view; you would instead make a
new list without that element).

Lets break this out into some a function to solve the problem.  I'll
leave the implementation to you.

removeElement :: [String] -> Int -> (String, [String])

This is a pure function, but you are, as you mentioned, inside IO.
But that's no problem; in your higher level code (where you call
parseImageFile) you should also be inside IO:

main = do
     images <- parseImageFile path
     -- now, images :: [String]
     -- simple!  you are living inside of IO here, so you can get at
the values returned!

     n <- getRandomNumber (length images)
     let (image, rest) = removeElement images n
     changeDesktop image

Note the difference between the lines with "<-" and the line with
"let"; "x <- m" takes "m :: IO a" and gives you an "x :: a".  Whereas
"let x = z" takes "z :: a" and gives "x :: a"; it just labels the
result of a non-IO call.

Now you just have to write the other functions:

getRandomNumber :: Int -> IO Int
changeDesktop :: String -> IO ()
path :: String  -- not really a function!

To loop, you can write an IO action that calls itself as the last
element of the "do" statement.

  -- ryan

More information about the Haskell-Cafe mailing list