Skip to content

Instantly share code, notes, and snippets.

@coproduto
Last active September 11, 2020 00:07
Show Gist options
  • Save coproduto/3c73a8da7568e0b8398ad024d614938d to your computer and use it in GitHub Desktop.
Save coproduto/3c73a8da7568e0b8398ad024d614938d to your computer and use it in GitHub Desktop.
data ReadState = None | Input | Output
split :: (Char -> Bool) -> String -> [String]
split f s = let (prefix, rest) = break s in
case rest of
[] -> [prefix]
[c] -> [prefix]
(c:cs) -> prefix : (split f cs)
main :: IO ()
main = do
handle <- openFile "nome.do.arquivo"
contents <- hGetContents handle
processLines None (lines contents)
processLines :: [String] -> ReadState -> IO ()
processLines [] _ = pure ()
processLines (line:rest) state =
let [code, value] = split (== '|') line
newState = processLine state code value in
processLines rest newState
processLine :: ReadState -> String -> String -> ReadState
processLine _ "C100" status
| status == "entrada" = Input
| status == "saida" = Output
| otherwise = error "Linha inválida!"
processLine _ _ _ = ... -- tratar os outros casos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment