Browse Source

Add the div 0 protection

DricomDragon 5 years ago
parent
commit
6bd0118d65
1 changed files with 4 additions and 4 deletions
  1. 4 4
      functor.hs

+ 4 - 4
functor.hs

@@ -11,19 +11,19 @@ evaluate (Neg e) = negate (evaluate e)
 
 -- data Maybe a = Nothing | Just a
 
-isZero::Maybe a -> Bool
+isZero::(Num a, Eq a)=>Maybe a -> Bool
 isZero m = case m of 
 	Nothing -> True
-	_ -> False
+	Just x -> if x == 0 then True else False
 
 mfmap::(Fractional a) => (a -> b) -> Maybe a -> Maybe b
 mfmap f m = case m of
 	Nothing -> Nothing
 	Just x -> Just (f x)
 
-safeval::(Fractional a) => Expr a -> Maybe a
+safeval::(Fractional a, Eq a) => Expr a -> Maybe a
 safeval (Val x) = Just x
 safeval (Inc e) = mfmap (+ 1) (safeval e)
 safeval (Dec e) = mfmap (subtract 1) (safeval e)
-safeval (Inv e) = mfmap (1 /) (safeval e)
+safeval (Inv e) = let v = safeval e in if isZero v then Nothing else mfmap (1 /) v
 safeval (Neg e) = mfmap negate (safeval e)