From: Ramakrishnan Muthukrishnan Date: Sun, 21 Dec 2014 10:51:28 +0000 (+0530) Subject: hw5: exercise 1 and 2 X-Git-Url: https://git.rkrishnan.org/pf/content/en/footer/frontends/CLI.rst?a=commitdiff_plain;h=305e0c26a007817bb0194358bdb52ad5092e6371;p=yorgey.git hw5: exercise 1 and 2 --- diff --git a/hw5/Calc.hs b/hw5/Calc.hs new file mode 100644 index 0000000..ef4fd2e --- /dev/null +++ b/hw5/Calc.hs @@ -0,0 +1,20 @@ +{-# OPTIONS_GHC -Wall #-} + +module Calc where + +import ExprT +import Parser + +eval :: ExprT -> Integer + +-- exercise 1 +eval (Lit n) = n +eval (Add e1 e2) = eval e1 + eval e2 +eval (Mul e1 e2) = eval e1 * eval e2 + +-- exercise 2 +evalStr :: String -> Maybe Integer +-- evalStr s = case (parseExp Lit Add Mul s) of +-- Just e -> Just (eval e) +-- Nothing -> Nothing +evalStr s = fmap eval (parseExp Lit Add Mul s)