Skip to content

Instantly share code, notes, and snippets.

Created April 11, 2013 21:33
Show Gist options
  • Save anonymous/5367373 to your computer and use it in GitHub Desktop.
Save anonymous/5367373 to your computer and use it in GitHub Desktop.
stdin
{-# LANGUAGE FunctionalDependencies,UndecidableInstances,FlexibleInstances #-}
class ReadObj a p | a -> p where
readObj :: p -> [[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 pa, ReadObj b pb) => ReadObj (a,b) (pa, pb) 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 pa) => ReadObj [a] (Integer, [pa]) 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