[Haskell-beginners] More Deserialization Woes

Stephen Tetley stephen.tetley at gmail.com
Thu Jul 1 07:17:51 EDT 2010


Hi

As Benjamin Edwards says you want to be in the IO monad as you are
using a file handle

Something like this - note the last two lines have changed a bit to
accommodate the computation being in the IO monad rather than pure, if
you have very large input data you might need to put this in tail
recursive form.

readNames :: Int -> Handle -> IO [String]
readNames 0 _ = return []  -- add a return, to lift the answer into IO
readNames n h = do
  length <- fmap (fromIntegral . runGet getWord32be) $ L.hGet h 4
  name <- L.hGet h length
  rest <- readNames (n-1) h
  return ((UTF.toString name) : rest)


More information about the Beginners mailing list