-
-
Save dmwit/5367461 to your computer and use it in GitHub Desktop.
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
{-# LANGUAGE TypeFamilies #-} | |
type family StateFor a | |
type instance StateFor Integer = () | |
type instance StateFor (a, b) = (StateFor a, StateFor b) | |
type instance StateFor [a] = (Integer, [StateFor a]) | |
class ReadObj a where | |
readObj :: StateFor a -> [[String]] -> (a, [[String]]) | |
consNE :: [a] -> [[a]] -> [[a]] | |
consNE [] t = t | |
consNE h t = h:t | |
-- grab an integer off of the stream | |
instance ReadObj Integer where | |
readObj _ ((h:t):t') = (read h, consNE t t') | |
-- read a pair of objects, make sure they are on the same line. | |
instance (ReadObj a, ReadObj b) => ReadObj (a,b) where | |
readObj (pa,pb) (h:rest) = let (a,[t]) = readObj pa [h] | |
(b,[t']) = readObj pb [t] in | |
((a,b),consNE t' rest) | |
-- should assume that the entire list is going to be on one line | |
instance ReadObj a => ReadObj [a] where | |
readObj (0, _) l = ([], l) | |
readObj (n, (ph:pt)) l = | |
let (retH, listTail) = (readObj) (ph) l | |
(retT, realTail) = readObj (n-1, pt) listTail in | |
(retH:retT, realTail) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment