{{ message }}

Instantly share code, notes, and snippets.

# Patrick Thomsonpatrickt

🗣️
play stupid games / win stupid prizes
Last active Aug 29, 2015
View parameterized_expr.hs
 data Expr a = Index a a | Call [a] | Unary String a | Binary a String a | Paren a | Literal Lit deriving (Show, Eq)
Last active Aug 29, 2015
View flatten_expr.hs
 -- this would turn the expression -- (((anArray[(10)]))) -- into -- anArray flatten :: Expr -> Expr -- base case: do nothing to constants flatten (Constant i) = Constant i -- this is the important case: we shed the Paren constructor and just -- apply `flatten` to its contents
Last active Aug 29, 2015
simple syntax tree
View simple_syntax.hs
 data Lit = StrLit String | IntLit Int | Ident String deriving (Show, Eq) data Expr = Index Expr Expr | Call [Expr] | Unary String Expr
Created Feb 15, 2014
View apply.hs
 -- 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
Created Feb 15, 2014
printf example
View printf_example.hs
 -- the representation of `printf("2 + 3 is %d", 2 + 3)` printfExample :: Stmt printfExample = Expression (Call (Constant (Ident "printf")) [ Constant (StrLit "2 + 3 is %d") , Binary (Constant (IntLit 2)) "+"
Created Feb 15, 2014
View functor.hs
 class Functor f where fmap :: Functor f => (a -> b) -> f a -> f b
Created Feb 18, 2014
View fix.hs
 fix :: (a -> a) -> a fix f = f (fix f)
Last active Aug 29, 2015
View term.hs
 newtype Term f = In { out :: f (Term f) } )
Created Feb 17, 2014
View exprtree.hs
 -- not legal Haskell! type ExprTree = Expr (Expr (Expr (Expr ...)))
Created Feb 19, 2014
View expr_functor.hs
 {-# LANGUAGE DeriveFunctor #-} data Expr a = Index a a | Call [a] | Unary String a | Binary a String a | Paren a | Literal Lit deriving (Show, Eq, Functor) -- fmap for free
You can’t perform that action at this time.