Skip to content

Instantly share code, notes, and snippets.

@tomerfiliba
Created August 17, 2011 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomerfiliba/1152878 to your computer and use it in GitHub Desktop.
Save tomerfiliba/1152878 to your computer and use it in GitHub Desktop.
My first Haskell program
{- learning haskell by "porting" Construct from python -}
import Data.Int
import Data.Word
import qualified Data.ByteString.Lazy as LBS
import Data.Char
import Control.Monad
import Data.Binary.Get
data Packer t = Packer {
unpack :: Get t
}
bytes :: Int64 -> Packer LBS.ByteString
bytes len = Packer {
unpack = do
bs <- getLazyByteString len
return bs
}
struct :: [Packer t] -> Packer [t]
struct packers = Packer {
unpack = do
res <- mapM unpack packers -- that was a wild guess, but it works :)
return res
}
x = struct [bytes 4, bytes 3]
main :: IO ()
main = do
print $ runGet (unpack $ x) rawData
where
rawData = LBS.pack . map (fromIntegral . ord) $ "AAAABBBBCCCC"
-- prints ["AAAA", "BBB"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment