Skip to content

Instantly share code, notes, and snippets.

@takahisa
Last active January 17, 2017 00:25
Show Gist options
  • Save takahisa/c67cdd4c7ddf390efbe83684d66519e2 to your computer and use it in GitHub Desktop.
Save takahisa/c67cdd4c7ddf390efbe83684d66519e2 to your computer and use it in GitHub Desktop.
Scrap_Your_Boilerplate_1
{-# LANGUAGE DeriveDataTypeable #-}
module Main where
import Data.Generics
import Data.Data
import Data.Typeable
data Exp = Add Exp Exp | Sub Exp Exp | Val Int
deriving (Show, Eq, Typeable, Data)
eval :: Exp -> Exp
eval = everywhere (mkT eval')
where
eval' (Add (Val n1) (Val n2)) = Val (n1 + n2)
eval' (Sub (Val n1) (Val n2)) = Val (n1 - n2)
eval' e = e
main :: IO ()
main = do
putStrLn $ show (eval (Add (Val 1) (Val 2)))
putStrLn $ show (eval (Add (Val 1) (Sub (Val 2) (Val 3))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment