Haskell monad: IO [Double] to [IO Double]?

If you get a segmentation fault error, and you didn't use the FFI or any functions with unsafe in their name, that's not unsurprising, in any situation! It means there's a bug with either GHC, or a library you're using is doing something unsafe.

Up vote 7 down vote favorite 1 share g+ share fb share tw.

Consider the following code that is supposed to print out random numbers: import System.Random. Mersenne main = do g Suppose I wanted to print out only the first ten values of xs. How could I do that?

Xs has type IO Double, and I think I want a variable of type IO Double. What operators exist to convert between the two. Haskell random io monads link|improve this question edited Apr 5 at 1:54Philip JF3,043612 asked Apr 4 at 19:25Gautam1597 15% accept rate.

Incidentally, IO Double -> IO Double is essentially the reverse type signature of 'sequence'. – Gautam Apr 4 at 19:27 3 It doesn't segfault here. – Daniel Wagner Apr 4 at 19:42 1 Sounds like a miscompilation of some kind or a hardware issue, then... you might want to run a memtest86+ check.

– ehird Apr 4 at 19:44 1 You could do a IO Double -> IO IO Double ... unless you unsafePerformIO. – trinithis Apr 4 at 19:51 2 That stray -} is quite unusual. – Dan Burton Apr 4 at 20:56.

If you get a segmentation fault error, and you didn't use the FFI or any functions with unsafe in their name, that's not unsurprising, in any situation! It means there's a bug with either GHC, or a library you're using is doing something unsafe. Printing out an infinite list of Doubles with mapM_ print is perfectly fine; the list will be processed incrementally and the program should run with constant memory usage.

I suspect there is a bug in the System.Random. Mersenne module you're using, or a bug the C library it's based on, or a problem with your computer (such as faulty RAM).1 Note that newMTGen comes with this warning: Due to the current SFMT library being vastly impure, currently only a single generator is allowed per-program. Attempts to reinitialise it will fail.

You might be better off using the provided global MTGen instead. That said, you can't convert IO Double into IO Double in that way; there's no way to know how long the resulting list would be without executing the IO action, which is impossible, since you have a pure result (albeit one that happens to contain IO actions). For infinite lists, you could write: desequence :: IO a -> IO a desequence = desequence' 0 where desequence n m = fmap (! n) m : desequence (n+1) m But every time you execute an action in this list, the IO a action would be executed again; it'd just discard the rest of the list.

The reason randoms can work and return an infinite list of random numbers is because it uses lazy IO with unsafeInterleaveIO. (Note that, despite the "unsafe" in the name, this one can't cause segfaults, so something else is afoot. ) 1 Other, less likely possibilities include a miscompilation of the C library, or a bug in GHC.

3 Just for the record, I think it's possible something is wrong with the questioner's computer, not with the library; the supplied code doesn't segfault for me. – Daniel Wagner Apr 4 at 19:43 1 +1 for "you can't convert IO Double into IO Double... there's no way to know how long the resulting list would be without executing the IO action" – Dan Burton Apr 4 at 20:56 So there is no way to access just the first ten list elements? – Gautam Apr 4 at 21:51 2 @Gautam: Sure there is, just use take on the resulting list: mapM_ print (take 10 xs).

– ehird Apr 4 at 22:00 @DanielWagner, you are correct, when I recompiled the mersenne-random package without using the -f use sse_2 flag, it worked without a segfault. That is slightly worrying, since the -f use sse_2 flag is supposed to be set so that the module can take advantage of SIMD optimization. – Gautam Apr 47 at 1:08.

Suppose I wanted to print out only the first ten values of xs. How could I do that? Just use take: main = do g You could also skip the binding using liftM: main = do g.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions