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

Patrik Iselind patrik.mrx at gmail.com
Fri Nov 24 21:32:17 UTC 2017


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20171124/992ceca1/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: exercises.hs
Type: text/x-haskell
Size: 987 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20171124/992ceca1/attachment.hs>


More information about the Beginners mailing list