aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordan <[email protected]>2021-04-11 12:32:23 +0200
committerdan <[email protected]>2021-04-11 12:32:23 +0200
commit90dd6dfdb47edc09f25fbb9b83dbe7f89b179492 (patch)
tree7e7a28d0a3d8d58dd645feba26f64873ae8589a9
parent1eca5369046f242d745f64a836c2f5720e2a7b71 (diff)
downloadbizexp-90dd6dfdb47edc09f25fbb9b83dbe7f89b179492.tar.gz
bizexp-90dd6dfdb47edc09f25fbb9b83dbe7f89b179492.tar.bz2
bizexp-90dd6dfdb47edc09f25fbb9b83dbe7f89b179492.zip
add eAny function
-rw-r--r--src/MyLib.hs31
1 files changed, 25 insertions, 6 deletions
diff --git a/src/MyLib.hs b/src/MyLib.hs
index cc84930..5e5a617 100644
--- a/src/MyLib.hs
+++ b/src/MyLib.hs
@@ -16,12 +16,24 @@ eval :: String -> Maybe Integer
eval x = coerceToInt =<< evalAst.head.fst =<< parseLevel x
-data Value = IntVal Integer | StrVal String
+data Value = IntVal Integer | StrVal String | BoolVal Bool
deriving (Show)
+class CoerceTo a where
+ coerceTo :: Value -> Maybe a
+
coerceToInt :: Value -> Maybe Integer
coerceToInt (IntVal n) = Just n
coerceToInt (StrVal s) = readMaybe s
+coerceToInt (BoolVal True) = Just 1
+coerceToInt (BoolVal False) = Just 0
+
+coerceToBool :: Value -> Maybe Bool
+coerceToBool (IntVal v) = Just $ v /= 0
+coerceToBool (StrVal "True") = Just True
+coerceToBool (StrVal "true") = Just True
+coerceToBool (BoolVal v) = Just v
+coerceToBool _ = Just False
type Name = String
@@ -68,11 +80,9 @@ evalLevel = mapM evalAst
getFunc :: String -> Maybe ([Value] -> Maybe Value)
-getFunc _ = Just eSum
-
-
--- (name, (func, minParams))
-funcs0 = [("sum",(eSum, 0))]
+getFunc "sum" = Just eSum
+getFunc "any" = Just eAny
+getFunc _ = Nothing
eSum :: [Value] -> Maybe Value
eSum [] = Just $ IntVal 0
@@ -83,3 +93,12 @@ eSum (x:xs) =
in
IntVal <$> ((+) <$> a1 <*> a0)
+eAny :: [Value] -> Maybe Value
+eAny [] = Just $ BoolVal False
+eAny (x:xs) =
+ let
+ a1 = coerceToBool =<< eAny xs
+ a0 = coerceToBool x
+ in
+ BoolVal <$> ((||) <$> a1 <*> a0)
+