Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
-- decode.enable1.stm.hs
-- inspired by a word guessing game
-- https://www.futurelearn.com/courses/functional-programming-haskell/1/steps/116475
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString as B
import Data.Binary.Get
import Data.Word
-- readStem :: Get (Int, Int, Data.ByteString.Internal.ByteString)
readStem = do
prefixLength <- fmap fromIntegral getWord8
suffixLength <- fmap fromIntegral getWord8
suffix <- getByteString suffixLength
return (prefixLength, suffixLength, suffix)
-- https://wiki.haskell.org/Dealing_with_binary_data
readStems = do
empty <- isEmpty
if empty then return []
else do v <- readStem
rest <- readStems
return (v : rest)
mkWords stems = scanl accum B.empty stems where
accum acc (pl,sl,suf) = B.concat [(B.take pl acc), suf]
main = do
let fname = "enable1.stm"
input <- BL.readFile fname
let stems = runGet readStems input
let words = mkWords stems
mapM_ B.putStrLn words
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment