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

Quentin Liu quentin.liu.0415 at gmail.com
Fri Nov 24 19:04:39 UTC 2017


> > > 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]?

Could you copy and paste the error message here?

The type signature `a` means it could be anything, `String`, `[String]`, or any ADT you could come up with. So in a type signature if you write
  func :: a -> a -> a
     func a b = a
this funciton is telling ghc that I have a function that accepts two parameters that must be of the same type, whatever the type is. So `a` could be an ADT, a list, a list of lists, etc. But if you write
  func :: a -> [b] -> a
  func a bs = a
you are essentially saying this function would only take two parameters of two types (`a` and `b` could be of the same type) and the second parameter must be a list. This, however, does not suggest mean that `[b]` could not be `[[String]]`, for `[String]` could just be thought of as a `b`. The way I use to think about type signature is, when you trying to substitute type variables such as `a`, substitute it into a concrete type that you are working with.

Regards,
Qingbo Liu

On Nov 23, 2017, 03:19 -0500, mrx <patrik.mrx at gmail.com>, wrote:
> > On Wed, Nov 22, 2017 at 10:40 PM, Quentin Liu <quentin.liu.0415 at 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
> _______________________________________________
> 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/20171124/14b82385/attachment-0001.html>


More information about the Beginners mailing list