
Pankaj Godbole's code examples for 8.9.6 are sufficiently more complex than mine,that I am worried I missed an obvious bug I'm not experience enough to know about or test for. Correction or reassurances would be appreciated. Regards, Trent {-- 5. Given the type declaration data Expr = Val Int | Add Expr Expr define a higher-order function folde :: (Int -> a) -> (a -> a -> a) -> Expr -> a such that folde f g replaces each Val constructor in an expression by the function f, and each Add constructor by the function g. Hutton, Graham. Programming in Haskell (Kindle Locations 3364-3371). Cambridge University Press. Kindle Edition. --} data Expr = Val Int | Add Expr Expr -- Trent's stuff folde :: (Int -> a) -> (a -> a -> a) -> Expr -> a folde f g (Val x) = f x folde f g (Add left right) = g (folde f g left) (folde f g right) {-- 6. Using folde, define a function eval :: Expr -> Int that evaluates an expression to an integer value, and a function size :: Expr -> Int that calculates the number of values in an expression. Hutton, Graham. Programming in Haskell (Kindle Locations 3372-3374). Cambridge University Press. Kindle Edition. --} -- Trent's stuff eval :: Expr -> Int eval = folde id (+) size :: Expr -> Int size = folde (\_ -> 1) (+) -- hlint suggests \_ as const 1 test0 :: Expr test0 = Val 1 test1 :: Expr test1 = Add (Val 1) (Val 2) test3 :: Expr test3 = Add (Val 1) (Add (Val 2) (Val 3)) -- the below are from: -- Pankaj Godbole -- https://github.com/pankajgodbole/hutton/blob/master/exercises.hs evalE :: Expr -> Int evalE (Val n) = n evalE (Add e1 e2) = folde toEnum (+) (Add e1 e2) -- doesn't a Val have to prefix an int? Then why is f "toEnum"? numVals :: Expr -> Int numVals (Val n) = 1 numVals (Add e1 e2) = numVals e1 + numVals e2

Your answers are good. toEnum is redundant. Reconstructing the Add is
unnecessary. His second function is fine but it doesn't use folde.
I would take hlint's suggestion and use const. This situation is a common
use case.
On Fri, Sep 14, 2018 at 4:04 AM trent shipley
Pankaj Godbole's code examples for 8.9.6 are sufficiently more complex than mine,that I am worried I missed an obvious bug I'm not experience enough to know about or test for.
Correction or reassurances would be appreciated.
Regards,
Trent
{--
5. Given the type declaration
data Expr = Val Int | Add Expr Expr
define a higher-order function
folde :: (Int -> a) -> (a -> a -> a) -> Expr -> a
such that folde f g replaces each Val constructor in an expression by the function f, and each Add constructor by the function g.
Hutton, Graham. Programming in Haskell (Kindle Locations 3364-3371). Cambridge University Press. Kindle Edition.
--}
data Expr = Val Int | Add Expr Expr
-- Trent's stuff
folde :: (Int -> a) -> (a -> a -> a) -> Expr -> a folde f g (Val x) = f x folde f g (Add left right) = g (folde f g left) (folde f g right)
{--
6. Using folde, define a function
eval :: Expr -> Int
that evaluates an expression to an integer value, and a function
size :: Expr -> Int
that calculates the number of values in an expression.
Hutton, Graham. Programming in Haskell (Kindle Locations 3372-3374). Cambridge University Press. Kindle Edition.
--}
-- Trent's stuff
eval :: Expr -> Int eval = folde id (+)
size :: Expr -> Int size = folde (\_ -> 1) (+) -- hlint suggests \_ as const 1
test0 :: Expr test0 = Val 1
test1 :: Expr test1 = Add (Val 1) (Val 2)
test3 :: Expr test3 = Add (Val 1) (Add (Val 2) (Val 3))
-- the below are from: -- Pankaj Godbole -- https://github.com/pankajgodbole/hutton/blob/master/exercises.hs
evalE :: Expr -> Int evalE (Val n) = n evalE (Add e1 e2) = folde toEnum (+) (Add e1 e2) -- doesn't a Val have to prefix an int? Then why is f "toEnum"?
numVals :: Expr -> Int numVals (Val n) = 1 numVals (Add e1 e2) = numVals e1 + numVals e2 _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (2)
-
David McBride
-
trent shipley