[Haskell-beginners] Why do i need to specify the class of a here at all?

Quentin Liu quentin.liu.0415 at gmail.com
Wed Nov 22 21:40:13 UTC 2017


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.mrx at 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 at 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.html>


More information about the Beginners mailing list