[Haskell-beginners] IO ( stuff )

Paul Monday paul.monday at parsci.com
Fri Dec 9 21:00:57 CET 2011


I've hammered through the bulk of my issues as I try to build a matrix that consists of random numbers, but the final hurdle is mixing pure and impure functions.  Does "impurity" from something like a random number generator or file I/O have to move it's way all the way through my code?

Here is the specific example.  I have a function that makes a row in a matrix, it looks like this:
makerow :: Int -> (Float, Float) -> IO (U.Vector Float)

The IO (U.Vector Float) is the result of the random number generation that is an impure call, making this an impure result.

I want to compose this into a list
makerows :: Int -> Int -> (Float, Float) -> [U.Vector Float]
makerows 0 _ _ = []
makerows r n range = makerow n range : makerows r' n range
    where r' = r - 1

But, of course, I can't mix the IO (U.Vector Float) with a U.Vector Float

The compilation result in:
    Couldn't match expected type `U.Vector Float'
                with actual type `IO (U.Vector Float)'
    In the return type of a call of `makerow'

So, at some point, I have to lift I believe … is there a simple lifting solution?  It initially seemed that Monads were the solution … but liftIO resulted in the same thing … just with Monad (U.Vector Float) … 

There is just some "simple" Haskellism I'm missing here, but after an ginormous amount of reading and googling, it is still eluding me :(

Any thoughts (besides the one page I found that helpfully … basically … said "Go back to Java" ;-)

Paul Monday
Parallel Scientific, LLC.
paul.monday at parsci.com







More information about the Beginners mailing list