So this is the data definition and my function.
Task 3
Define addAll, which adds a list of expressions together into a single expression without introducing any 'junk'.
You could use foldr since it reduces a list into a single expression.
Data definition and function
data Expr = Op BinOp Expr Expr | NumLit Int | ExpX Int deriving (Eq, Show) data BinOp = AddOp | MulOp deriving (Eq, Show) addAll :: [Expr] -> Expr addAll exprs = foldr(\x acc -> if null acc then x else add x acc) empty where empty = NumLit 3 Error message
src/Simplify.hs:31:16: error: [GHC-83865] • Couldn't match expected type ‘Expr’ with actual type ‘t0 (t1 a0) -> t1 a0’ • Probable cause: ‘foldr’ is applied to too few arguments In the expression: foldr (\ x acc -> if null acc then x else add x acc) empty In an equation for ‘addAll’: addAll exprs = foldr (\ x acc -> if null acc then x else add x acc) empty where empty = NumLit 3 | 31 | addAll exprs = foldr(\x acc -> if null acc then x else add x acc) empty | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ src/Simplify.hs:31:56: error: [GHC-83865] • Couldn't match expected type ‘t1 a0’ with actual type ‘Expr’ • In the expression: add x acc In the expression: if null acc then x else add x acc In the first argument of ‘foldr’, namely ‘(\ x acc -> if null acc then x else add x acc)’ • Relevant bindings include acc :: t1 a0 (bound at src/Simplify.hs:31:25) x :: t1 a0 (bound at src/Simplify.hs:31:23) | 31 | addAll exprs = foldr(\x acc -> if null acc then x else add x acc) empty | ^^^^^^^^^ src/Simplify.hs:31:60: error: [GHC-83865] • Couldn't match expected type ‘Expr’ with actual type ‘t1 a0’ • In the first argument of ‘add’, namely ‘x’ In the expression: add x acc In the expression: if null acc then x else add x acc • Relevant bindings include acc :: t1 a0 (bound at src/Simplify.hs:31:25) x :: t1 a0 (bound at src/Simplify.hs:31:23) | 31 | addAll exprs = foldr(\x acc -> if null acc then x else add x acc) empty | ^ src/Simplify.hs:31:62: error: [GHC-83865] • Couldn't match expected type ‘Expr’ with actual type ‘t1 a0’ • In the second argument of ‘add’, namely ‘acc’ In the expression: add x acc In the expression: if null acc then x else add x acc • Relevant bindings include acc :: t1 a0 (bound at src/Simplify.hs:31:25) x :: t1 a0 (bound at src/Simplify.hs:31:23) | 31 | addAll exprs = foldr(\x acc -> if null acc then x else add x acc) empty | ^^^ src/Simplify.hs:31:67: error: [GHC-83865] • Couldn't match expected type ‘t1 a0’ with actual type ‘Expr’ • In the second argument of ‘foldr’, namely ‘empty’ In the expression: foldr (\ x acc -> if null acc then x else add x acc) empty In an equation for ‘addAll’: addAll exprs = foldr (\ x acc -> if null acc then x else add x acc) empty where empty = NumLit 3 | 31 | addAll exprs = foldr(\x acc -> if null acc then x else add x acc) empty | Terminal output
Error: [Cabal-7125] Failed to build Simplify-0.1.0.0 (which is required by exe:mark from Simplify-0.1.0.0 and test:SimplifyTest from Simplify-0.1.0.0). charlotte@Charlotte-DELL:~/functional/CW2-Simplify$ #
foldr1, not justfoldr.exprsargument to thefoldr.