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

mrx patrik.mrx at gmail.com
Thu Nov 23 08:22:50 UTC 2017


On Wed, Nov 22, 2017 at 10:47 PM, Francesco Ariis <fa-ml at 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-0001.html>


More information about the Beginners mailing list