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

Quentin Liu quentin.liu.0415 at gmail.com
Sun Nov 26 19:48:22 UTC 2017


> ```
> exercises.hs:33:13:
>     Couldn't match expected type ‘[b0]’ with actual type ‘a’
>       ‘a’ is a rigid type variable bound by
>           the type signature for myOrderFunc :: a -> a -> Ordering
>           at exercises.hs:31:16
>     Relevant bindings include
>       y :: a (bound at exercises.hs:32:15)
>       x :: a (bound at exercises.hs:32:13)
>       myOrderFunc :: a -> a -> Ordering (bound at exercises.hs:32:1)
>     In the first argument of ‘myLen’, namely ‘x’
>     In the first argument of ‘(<)’, namely ‘myLen x’
> Failed, modules loaded: none.
> ```

Your guess is correct. The problem is, Haskell does not consider `a` in `myOrderFunc` and `[b]` in `myLen` equivalent. `a` means you feed the function any type, while `[b]` means it must be a list of values of the same type. So changing `a` to `[a]` woud eliminate the error.

Regards,
Qingbo Liu

On Nov 24, 2017, 16:33 -0500, Patrik Iselind <patrik.mrx at gmail.com>, wrote:
>
> Den 2017-11-24 kl. 20:04, skrev Quentin Liu:
> >
> > > > > 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?
> Sure, the error i get follows
> ```
> exercises.hs:33:13:
>     Couldn't match expected type ‘[b0]’ with actual type ‘a’
>       ‘a’ is a rigid type variable bound by
>           the type signature for myOrderFunc :: a -> a -> Ordering
>           at exercises.hs:31:16
>     Relevant bindings include
>       y :: a (bound at exercises.hs:32:15)
>       x :: a (bound at exercises.hs:32:13)
>       myOrderFunc :: a -> a -> Ordering (bound at exercises.hs:32:1)
>     In the first argument of ‘myLen’, namely ‘x’
>     In the first argument of ‘(<)’, namely ‘myLen x’
> Failed, modules loaded: none.
> ```
> Attaching the updated exercises.hs for reference.
>
> I'm still not very good at interpreting Haskell's error messages, they are quite cryptic to me. My interpretation/guess of the above is that my `a` is too 'wide' or how you express it. Haskell seem to expect some form of list. Most likely since i want a length and lists are perhaps everything in Haskell that can produce a length. I've hardly scratched the surface of what i imagine is Haskell so i cannot say anything for sure yet.
>
> >
> > 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.
> I'm having a hard time understanding your way of thinking about type signatures. Could you perhaps elaborate a bit more on it?
>
> // 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/20171126/dd685676/attachment-0001.html>


More information about the Beginners mailing list