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. Date type needs helper function? (Galaxy Being) 2. Re: Date type needs helper function? (Matthew Low) ---------------------------------------------------------------------- Message: 1 Date: Thu, 1 Jul 2021 11:41:21 -0500 From: Galaxy Being <borg...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: [Haskell-beginners] Date type needs helper function? Message-ID: <cafahfsvei93bdfau_n0mhgebs_lqtumjonq9_am3bljoyfz...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" I am using the built-in data type Day (Data.Time) in two ways data PDate = PDate Day or type PDate Day doesn't seem to matter. But then this doesn't work testrec1 = PDate 2021 7 1 I always must use the "helper function" fromGregorian testrec0 = PDate (fromGregorian 2021 7 1) ... PDate 2021-07-01 Looking at Real World Haskell examples data BookInfo = Book Int String [String] deriving (Show) ... myInfo = Book 9780135072455 "Algebra of Programming" ["Richard Bird", "Oege de Moor"] I know there's a great Haskell lesson to learn here, so why can Book take everything naked but my Day version not? ⨽ Lawrence Bottorff Grand Marais, MN, USA borg...@gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210701/3b27e204/attachment-0001.html> ------------------------------ Message: 2 Date: Thu, 1 Jul 2021 11:41:11 -0600 From: Matthew Low <m...@ualberta.ca> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Date type needs helper function? Message-ID: <CAC=gtkz+v90t-r5hemw3kc5os_9i0frnujnhofjanu8rrgt...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" > why can Book take everything naked but my Day version not? The Day data type is actually just a newtype wrapper around an Int, which encodes the number of days since some point in the past - the docs more details: https://hackage.haskell.org/package/time-1.12/docs/Data-Time-Calendar.html. So to use the data constructor directly as in the Book example, you'd have to give it the number of days directly, something like ModifiedJulianDay 1234. The helper function gives a more convenient way to construct a Day. I believe this general approach of having functions to help you construct a data type is called 'smart constructors'. They're more common when the data constructors are not exported from a module, so the helper functions are the only way to create a value of that type - useful if you have some constraints you need enforced on your type. On Thu, Jul 1, 2021 at 10:41 AM Galaxy Being <borg...@gmail.com> wrote: > I am using the built-in data type Day (Data.Time) in two ways > > data PDate = PDate Day > > or > > type PDate Day > > doesn't seem to matter. But then this doesn't work > > testrec1 = PDate 2021 7 1 > > I always must use the "helper function" fromGregorian > > testrec0 = PDate (fromGregorian 2021 7 1) > ... > PDate 2021-07-01 > > Looking at Real World Haskell examples > > data BookInfo = Book Int String [String] deriving (Show) > ... > myInfo = Book 9780135072455 "Algebra of Programming" ["Richard Bird", > "Oege de Moor"] > > I know there's a great Haskell lesson to learn here, so why can Book take > everything naked but my Day version not? > > > ⨽ > Lawrence Bottorff > Grand Marais, MN, USA > borg...@gmail.com > _______________________________________________ > 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/20210701/1989032d/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 156, Issue 1 *****************************************