From 26d7927c35fae46213b7a5cd00154179c0203227 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 6 Oct 2020 21:41:27 -0700 Subject: [PATCH] Re-use a fold because Parisa may not have my HW1 file. --- HW2.fedorind.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/HW2.fedorind.hs b/HW2.fedorind.hs index deeead6..1c0e0eb 100644 --- a/HW2.fedorind.hs +++ b/HW2.fedorind.hs @@ -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