Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Hutton Ex 8.9.5 & Ex 8.9.6 (trent shipley) 2. Re: Hutton Ex 8.9.5 & Ex 8.9.6 (David McBride) ---------------------------------------------------------------------- Message: 1 Date: Fri, 14 Sep 2018 01:04:12 -0700 From: trent shipley <trent.ship...@gmail.com> To: Haskell Beginners <beginners@haskell.org> Subject: [Haskell-beginners] Hutton Ex 8.9.5 & Ex 8.9.6 Message-ID: <caeflybkksnw-jcwms3fzfc7taumsdbexko9guezptj+zqki...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180914/e91e0a44/attachment-0001.html> ------------------------------ Message: 2 Date: Fri, 14 Sep 2018 08:06:02 -0400 From: David McBride <toa...@gmail.com> To: Haskell Beginners <beginners@haskell.org> Subject: Re: [Haskell-beginners] Hutton Ex 8.9.5 & Ex 8.9.6 Message-ID: <CAN+Tr43=odzq---bb8pxnydjvwhp2+dm_oroawue_eunoik...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" 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 <trent.ship...@gmail.com> wrote: > 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 > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180914/81fe65ea/attachment-0001.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 123, Issue 5 *****************************************