소스 검색

Implement function to evaluate arithmetic expressions

DricomDragon 5 년 전
부모
커밋
d92b76b4e8
1개의 변경된 파일8개의 추가작업 그리고 1개의 파일을 삭제
  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)