summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdoardo La Greca2025-07-09 22:35:50 +0200
committerEdoardo La Greca2025-07-09 22:35:50 +0200
commit319643faf6baea1c44f68800cba1947799935d05 (patch)
tree24d668ac4e0bde3579085279009cd5f51085b3e6
parent9e4698b5e7904e7dafd62a2682713742fd3eb198 (diff)
add second exercise of lecture 5
-rw-r--r--lec05/Calc.hs8
1 files changed, 8 insertions, 0 deletions
diff --git a/lec05/Calc.hs b/lec05/Calc.hs
index 8051d76..cd6f408 100644
--- a/lec05/Calc.hs
+++ b/lec05/Calc.hs
@@ -1,6 +1,7 @@
module Calc where
import ExprT
+import Parser
-- Exercise 1
@@ -8,3 +9,10 @@ eval :: ExprT -> Integer
eval (Lit n) = n
eval (Add l r) = eval l + eval r
eval (Mul l r) = eval l * eval r
+
+-- Exercise 2
+
+evalStr :: String -> Maybe Integer
+evalStr s = case parseExp Lit Add Mul s of
+ Nothing -> Nothing
+ Just t -> Just (eval t)