Created
August 17, 2011 23:09
-
-
Save tomerfiliba/1152878 to your computer and use it in GitHub Desktop.
My first Haskell program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- 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