Skip to content

Instantly share code, notes, and snippets.

@mstksg
Last active August 10, 2017 06:31
Show Gist options
  • Save mstksg/cb4b8a82405a4d7f19878c1669465820 to your computer and use it in GitHub Desktop.
Save mstksg/cb4b8a82405a4d7f19878c1669465820 to your computer and use it in GitHub Desktop.
data Auto1 a b = A1 { runAuto :: a -> (b, Auto1 a b) }
data Auto2 a b = forall s. A2 s ((a, s) -> (b, s))
sumFrom1 :: Int -> Auto1 Int Int
sumFrom1 n = A1 $ \x -> (x + n, sumFrom1 (x + n))
sumFrom2 :: Int -> Auto2 Int Int
sumFrom2 n0 = A2 n0 $ \(x, n) -> (x + n, x + n)
oneTwo :: Auto1 a b -> Auto2 a b
oneTwo a0 = A2 a0 $ \(x, a) -> runAuto a x
twoOne :: Auto2 a b -> Auto1 a b
twoOne (A2 s0 f) = A1 $ \x -> let (y, s1) = f (x, s0)
in (y, twoOne (A s1 f))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment