Skip to content

Instantly share code, notes, and snippets.

@spockz
Created September 18, 2010 15:05
Show Gist options
  • Save spockz/585734 to your computer and use it in GitHub Desktop.
Save spockz/585734 to your computer and use it in GitHub Desktop.
module Main where
data Exp a = Const a
| Plus (Exp a) (Exp a)
type ExpAlgebra a r = (a -> r
,r -> r -> r)
foldExp :: ExpAlgebra a r -> Exp a -> r
foldExp (c, p) = f
where f (Const a) = c a
f (Plus l r) = p (f l) (f r)
test = Const 4 `Plus` Const 8
calc = foldExp (id, (+))
pprint :: Show a => Exp a -> String
pprint = foldExp (show,\ l r -> "(" ++ l ++ " + " ++ r ++ ")")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment