Re-use a fold because Parisa may not have my HW1 file.

This commit is contained in:
Danila Fedorin 2020-10-06 21:41:27 -07:00
parent 93604a3c07
commit 26d7927c35

View File

@ -5,8 +5,8 @@ module HW2 where
import HW1
-- Copied from my HW1 for your convenience!
fold :: (Int -> a) -> (a -> a -> a) -> (a -> a -> a) -> Expr -> a
fold f1 f2 f3 = rec
fold' :: (Int -> a) -> (a -> a -> a) -> (a -> a -> a) -> Expr -> a
fold' f1 f2 f3 = rec
where
rec (Lit i) = f1 i
rec (Add l r) = f2 (rec l) (rec r)
@ -35,7 +35,7 @@ fold f1 f2 f3 = rec
-- True
--
toRPN :: Expr -> String
toRPN = fold show (rpn "+") (rpn "*")
toRPN = fold' show (rpn "+") (rpn "*")
where rpn op l r = concat [l, " ", r, " ", op]
@ -82,7 +82,7 @@ fromRPN = head . foldl step [] . words
-- >>> eval (neg e2)
-- -65
--
neg = fold (Lit . negate) Add (Mul . neg) -- Not efficient, but short :^)
neg = fold' (Lit . negate) Add (Mul . neg) -- Not efficient, but short :^)
-- | Takes two expressions and returns an expression that evalautes to the