[Haskell-beginners] Evaluate behaviour
Tom Hobbs
tvhobbs at googlemail.com
Wed Jul 7 15:51:40 EDT 2010
Hi guys,
Thanks to everyone who helped me, I hit a milestone this evening and
finally got the (small) bit of functionality I was working on, working! A
huge "thank you" to everyone who has taken the time to use very small
words to explain things to me! (I just need to sort out some nicer error
handling now...)
Just one more question... for today.
Here's my code;
import Network
import System.IO
(hGetLine,hClose,hPutStrLn,hSetBuffering,BufferMode(..),Handle,stdout)
import Data.Bits
import Data.Binary
import Data.Binary.Put
import Data.Binary.Get
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.UTF8 as UTF
import Control.Monad
import Control.Exception (evaluate)
ping a t = do
h <- connectTo a (PortNumber t)
hSetBuffering h NoBuffering
L.hPut h (encode (0xFAB10000 :: Word32))
numStrings <- fmap (fromIntegral . runGet getWord64be) $ L.hGet h 8
names <- (fmap (runGet $ readStrings numStrings) $ L.hGetContents h)
evaluate names
hClose h
return names
readStrings :: Int -> Get [String]
readStrings n = replicateM n $ do
len <- getWord32be
name <- getByteString $ fromIntegral len
return $ UTF.toString name
This works in GHCi exactly as I want it to. Note the call to "evaluate"
in the ping function which allows me to close the handle before returning
the IO [String]. This behaviour prints out the list of Strings as read
from the handle in GHCi, exactly what I wanted it to do. Here's the
strange part;
If I remove the evalute line, and instead have;
names <- evaluate (fmap (runGet $ readStrings numStrings) $ L.hGetContents
h)
GHCI claims that this is okay, but when I call ping I don't get any
output. I don't get any errors either, but I would expect to see the same
list of Strings as before.
Can anyone explain to me why that happpens? I'm assuming it's something
to do with the "<-" magic, but I don't know what.
Thanks,
Tom
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
More information about the Beginners
mailing list