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. Why do i need to specify the class of a here at all? (Patrik Iselind) 2. Re: Why do i need to specify the class of a here at all? (Quentin Liu) 3. Re: Why do i need to specify the class of a here at all? (Francesco Ariis) 4. Re: Why do i need to specify the class of a here at all? (mrx) 5. Re: Why do i need to specify the class of a here at all? (mrx) ---------------------------------------------------------------------- Message: 1 Date: Wed, 22 Nov 2017 22:15:59 +0100 From: Patrik Iselind <patrik....@gmail.com> To: beginners@haskell.org Subject: [Haskell-beginners] Why do i need to specify the class of a here at all? Message-ID: <2132becd-3108-7048-35cd-802c143fb...@gmail.com> Content-Type: text/plain; charset="windows-1252"; Format="flowed" Hi, I'm following the Real World Haskell book and in chapter three came across the exercise to sort a list of lists. I attach what i have so far. My question is the following. Why do i need to add "Eq a =>" to the type definition of sortListOfLists? The type of a should be irrelevant as i see it, my function couldn't care less what the type of the contents of the lists in the list are, or? All my function cares about is the length of those lists in the list. The designator a in the type declaration for sortListOfLists denote the type of the contents in the lists of lists as i understand it. So a list of lists containing object i can test for equality. Another thought i have is if i really have to specify that i expect list of lists. Couldn't that be list of something else with a length, a string perhaps, just as well? In other words a type like sortListOfLists :: a -> a. As i see it that should be just as valid as the type of what i return is the same as that which i get as input regardless of it being a list or not. -- Patrik Iselind -------------- next part -------------- A non-text attachment was scrubbed... Name: exercises.hs Type: text/x-haskell Size: 1007 bytes Desc: not available URL: <http://mail.haskell.org/pipermail/beginners/attachments/20171122/4504ad3e/attachment-0001.hs> ------------------------------ Message: 2 Date: Wed, 22 Nov 2017 16:40:13 -0500 From: Quentin Liu <quentin.liu.0...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Why do i need to specify the class of a here at all? Message-ID: <63573bd3-f846-419d-af36-c8e2c9bae0e3@Spark> Content-Type: text/plain; charset="utf-8" Hi Patrik, The reason for the requirement of “Eq a” in your `sortListOfLists` is that you are calling myOrderFunc which carries the signature “Eq a”. If you remove the `Eq` declaration in `myOrderFunc` the compiler then would not complain about the absence of `Eq` in `sortListOfLists`. For a detailed explanation you could reference chapter 6 of Real World Haskell. Yes, you could pass the function a list of strings as well. A string is just a list of Chars. The type signature `a` does not restrict the range of types you could pass to the function. A list of lists of lists would even work. e.g. > ghci> sortListOfLists [["hello"], ["this", "is"], ["just", "a", "test”]] > [["hello"],["this","is"],["just","a","test"]] Regards, Qingbo Liu On Nov 22, 2017, 16:17 -0500, Patrik Iselind <patrik....@gmail.com>, wrote: > Hi, > > I'm following the Real World Haskell book and in chapter three came > across the exercise to sort a list of lists. I attach what i have so far. > > My question is the following. Why do i need to add "Eq a =>" to the type > definition of sortListOfLists? The type of a should be irrelevant as i > see it, my function couldn't care less what the type of the contents of > the lists in the list are, or? All my function cares about is the length > of those lists in the list. The designator a in the type declaration for > sortListOfLists denote the type of the contents in the lists of lists as > i understand it. So a list of lists containing object i can test for > equality. > > Another thought i have is if i really have to specify that i expect list > of lists. Couldn't that be list of something else with a length, a > string perhaps, just as well? In other words a type like sortListOfLists > :: a -> a. As i see it that should be just as valid as the type of what > i return is the same as that which i get as input regardless of it being > a list or not. > > -- > Patrik Iselind > > > _______________________________________________ > 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/20171122/03e11bca/attachment-0001.html> ------------------------------ Message: 3 Date: Wed, 22 Nov 2017 22:47:13 +0100 From: Francesco Ariis <fa...@ariis.it> To: beginners@haskell.org Subject: Re: [Haskell-beginners] Why do i need to specify the class of a here at all? Message-ID: <20171122214713.by7pw2dohlj7s...@x60s.casa> Content-Type: text/plain; charset=us-ascii Hello Patrik, On Wed, Nov 22, 2017 at 10:15:59PM +0100, Patrik Iselind wrote: > My question is the following. Why do i need to add "Eq a =>" to the type > definition of sortListOfLists? Like you guessed, it doesn't! Just change the signature of `myOrderFunc` to: myOrderFunc :: [a] -> [a] -> Ordering (Eq isn't needed, as the implementation shows). Remember that you can arbitrarily write more restrictive signatures than the inferred ones! It is useful to state your intent, in this case it serves no purpose, so it's correct to get rid of the `Eq` constraint. > Another thought i have is if i really have to specify that i expect list of > lists. Couldn't that be list of something else with a length, a string > perhaps, just as well? In other words a type like sortListOfLists :: a -> a. > As i see it that should be just as valid as the type of what i return is the > same as that which i get as input regardless of it being a list or not. `sortBy` signature is sortBy :: (a -> a -> Ordering) -> [a] -> [a] It demands a list of something (prelude Strings are lists too, specifically `[Char]`). That "something" is dictated by myOrderFunc (`[a]`), hence [[a]], no way around it. Was this clear? Inferring type while writing code can be a tad difficult at first, but then it becomes second nature -F ------------------------------ Message: 4 Date: Thu, 23 Nov 2017 09:18:05 +0100 From: mrx <patrik....@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Why do i need to specify the class of a here at all? Message-ID: <CANzOjBhikz-vNqGGp8zck0Sz9u=i4_j9qefus3km7-jab5q...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" On Wed, Nov 22, 2017 at 10:40 PM, Quentin Liu <quentin.liu.0...@gmail.com> wrote: > Hi Patrik, > > The reason for the requirement of “Eq a” in your `sortListOfLists` is that > you are calling myOrderFunc which carries the signature “Eq a”. If you > remove the `Eq` declaration in `myOrderFunc` the compiler then would not > complain about the absence of `Eq` in `sortListOfLists`. For a detailed > explanation you could reference chapter 6 of Real World Haskell. > Thanks a lot for the reference. I'll make sure to read that chapter soon. > > Yes, you could pass the function a list of strings as well. A string is > just a list of Chars. The type signature `a` does not restrict the range of > types you could pass to the function. > That seem strange to me. Wouldn't that mean that i could write the declaration of myOrderFunc as `myOrderFunc :: a -> a -> Ordering` as well? GHCI give me an error on this though so obviously it's wrong. I just don't see why. Why cannot a represent [b]? // Patrik -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20171123/9eb5ea0c/attachment-0001.html> ------------------------------ Message: 5 Date: Thu, 23 Nov 2017 09:22:50 +0100 From: mrx <patrik....@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Why do i need to specify the class of a here at all? Message-ID: <canzojbi8im-g9buvq6p04dqprvqguz6rpnoonvc_twyp0hq...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" On Wed, Nov 22, 2017 at 10:47 PM, Francesco Ariis <fa...@ariis.it> wrote: > Hello Patrik, > > On Wed, Nov 22, 2017 at 10:15:59PM +0100, Patrik Iselind wrote: > > My question is the following. Why do i need to add "Eq a =>" to the type > > definition of sortListOfLists? > > Like you guessed, it doesn't! Just change the signature of `myOrderFunc` > to: > > myOrderFunc :: [a] -> [a] -> Ordering > > (Eq isn't needed, as the implementation shows). > Remember that you can arbitrarily write more restrictive signatures > than the inferred ones! It is useful to state your intent, in this > case it serves no purpose, so it's correct to get rid of the `Eq` > constraint. > > > Another thought i have is if i really have to specify that i expect list > of > > lists. Couldn't that be list of something else with a length, a string > > perhaps, just as well? In other words a type like sortListOfLists :: a > -> a. > > As i see it that should be just as valid as the type of what i return is > the > > same as that which i get as input regardless of it being a list or not. > > `sortBy` signature is > > sortBy :: (a -> a -> Ordering) -> [a] -> [a] > > It demands a list of something (prelude Strings are lists too, specifically > `[Char]`). That "something" is dictated by myOrderFunc (`[a]`), hence > [[a]], no way around it. > > Was this clear? Yes please, thank you. // Patrik -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20171123/1746c7de/attachment.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 113, Issue 20 ******************************************