Browse Source

Implement function to evaluate arithmetic expressions

DricomDragon 5 years ago
parent
commit
d92b76b4e8
1 changed files with 8 additions and 1 deletions
  1. 8 1
      functor.hs

+ 8 - 1
functor.hs

@@ -1,3 +1,10 @@
 -- Slide 64
 
-Expr a = Val a | Inc (Expr a) | Dec (Expr a) | Inv (Expr a) | Neg (Expr a)
+data Expr a = Val a | Inc (Expr a) | Dec (Expr a) | Inv (Expr a) | Neg (Expr a)
+
+evaluate::(Fractional a)=>Expr a -> a
+evaluate (Val x) = x
+evaluate (Inc e) = 1 + evaluate e
+evaluate (Dec e) = (evaluate e) - 1
+evaluate (Inv e) = 1 / (evaluate e)
+evaluate (Neg e) = negate (evaluate e)