Re-use a fold because Parisa may not have my HW1 file.
This commit is contained in:
parent
93604a3c07
commit
26d7927c35
|
@ -5,8 +5,8 @@ module HW2 where
|
||||||
import HW1
|
import HW1
|
||||||
|
|
||||||
-- Copied from my HW1 for your convenience!
|
-- Copied from my HW1 for your convenience!
|
||||||
fold :: (Int -> a) -> (a -> a -> a) -> (a -> a -> a) -> Expr -> a
|
fold' :: (Int -> a) -> (a -> a -> a) -> (a -> a -> a) -> Expr -> a
|
||||||
fold f1 f2 f3 = rec
|
fold' f1 f2 f3 = rec
|
||||||
where
|
where
|
||||||
rec (Lit i) = f1 i
|
rec (Lit i) = f1 i
|
||||||
rec (Add l r) = f2 (rec l) (rec r)
|
rec (Add l r) = f2 (rec l) (rec r)
|
||||||
|
@ -35,7 +35,7 @@ fold f1 f2 f3 = rec
|
||||||
-- True
|
-- True
|
||||||
--
|
--
|
||||||
toRPN :: Expr -> String
|
toRPN :: Expr -> String
|
||||||
toRPN = fold show (rpn "+") (rpn "*")
|
toRPN = fold' show (rpn "+") (rpn "*")
|
||||||
where rpn op l r = concat [l, " ", r, " ", op]
|
where rpn op l r = concat [l, " ", r, " ", op]
|
||||||
|
|
||||||
|
|
||||||
|
@ -82,7 +82,7 @@ fromRPN = head . foldl step [] . words
|
||||||
-- >>> eval (neg e2)
|
-- >>> eval (neg e2)
|
||||||
-- -65
|
-- -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
|
-- | Takes two expressions and returns an expression that evalautes to the
|
||||||
|
|
Loading…
Reference in New Issue
Block a user