Skip to content

Instantly share code, notes, and snippets.

@dmwit
Forked from anonymous/stdin.txt
Last active December 16, 2015 03:09
Show Gist options
  • Save dmwit/5367461 to your computer and use it in GitHub Desktop.
Save dmwit/5367461 to your computer and use it in GitHub Desktop.
{-# 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