[Haskell-beginners] Hints on how to remove unsafePerformIO from my function?

David McBride dmcbride at neondsl.com
Mon Jul 19 11:18:28 EDT 2010


Okay, I figured this out on my own.  This isn't as elegant but it definitely
works:

fetchUDPSIP :: TChan a -> TChan B.ByteString -> IO ()
fetchUDPSIP commands chan = do
  sock <- getUDPSocket 5060
  loop sock
  where
    loop sock = do
      result <- getUDP sock
      (atomically . writeTChan chan) result
      loop sock

fetchLine = do
  loop
  where
    loop = do
      result <- Prelude.getLine
      Prelude.putStrLn result
      loop


On Mon, Jul 19, 2010 at 11:09 AM, David McBride <dmcbride at neondsl.com>wrote:

> I tried this:
>
> fetchUDPSIP :: TChan a -> TChan B.ByteString -> IO ()
> fetchUDPSIP commands chan = do
>   sock <- getUDPSocket 5060
>   results <- (mapM unsafeInterleaveIO . repeat . getUDP) sock
>
>   mapM_ (atomically . writeTChan chan) results
>
> fetchLine = do
>   results <- (mapM unsafeInterleaveIO . repeat) Prelude.getLine
>   mapM_ Prelude.putStrLn results
>
> But it seems to have become strict again in that neither version will
> return any results until it has gotten everything it can.
>
> Also I'm not sure, but I'm pretty sure any "unsafe" function is still going
> to have the original problem where it causes all other threads to stop while
> it performs, so I don't think I can use unsafe anything at all and have my
> program work properly.
>
>
> On Mon, Jul 19, 2010 at 10:41 AM, Stephen Tetley <stephen.tetley at gmail.com
> > wrote:
>
>> How about unsafeInterleaveIO? - You might have to experiment where to
>> put it, but you shouldn't need the local unstrict definition.
>>
>> Because it doesn't violate type safety it is considered less heinous
>> than unsafePerformIO.
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100719/df669992/attachment.html


More information about the Beginners mailing list