Skip to content

Instantly share code, notes, and snippets.

@patrickt
Created February 15, 2014 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickt/9025675 to your computer and use it in GitHub Desktop.
Save patrickt/9025675 to your computer and use it in GitHub Desktop.
-- given a function f from a's to b's, `apply f` is a function that turns Expr a's into Expr b's
-- by mapping its argument over each subexpression of type a.
apply :: (a -> b) -> Expr a -> Expr b
-- The Constant constructor has no eligible subexpressions, so apply does nothing.
apply f (Constant ident) = (Constant ident)
-- Indexes have two children of type a; apply f to both of them.
apply f (Index x y) = Index (f x) (f y)
-- Calls have a list of arguments, so we map `apply f` over it
apply f (Call name args) = Call (f name) (map f args)
-- et cetera, et cetera
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment