Last active
January 17, 2017 00:25
-
-
Save takahisa/c67cdd4c7ddf390efbe83684d66519e2 to your computer and use it in GitHub Desktop.
Scrap_Your_Boilerplate_1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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