myLength :: [a] -> Int<br><br>This is the first type signature I
wrote. And I changed the Int into Num after<br>ghci tell me it's wrong.
This type signature still not work. But the standard length<br>function's
type signature is this:<br>
length :: [a] -> Int<br><br>I think my type signature is right but
it's not. And I can not find the reason.<br><br><div class="gmail_quote">2010/11/11 Chaddaï Fouché <span dir="ltr"><<a href="mailto:chaddai.fouche@gmail.com">chaddai.fouche@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div></div><div class="h5">On Thu, Nov 11, 2010 at 9:24 AM, 贾旭卿 <<a href="mailto:amazingjxq@gmail.com">amazingjxq@gmail.com</a>> wrote:<br>
> This is exercise 3.1 of Real World Haskell. I have my length function like<br>
> this:<br>
><br>
> myLength [] = 0<br>
> myLength (_:xs) = 1 + (myLength xs)<br>
><br>
> And I assumed the type signature is like this:<br>
> mylength :: [a] -> Num<br>
><br>
> But when I wrote this into the file and reloaded it into ghci, there is an<br>
> error.<br>
>><br>
>> The type signature for `mylength' lacks an accompanying binding<br>
>> Failed, modules loaded: none.<br>
><br>
><br>
> And the type signature given by ghci is<br>
>><br>
>> myLength :: (Num t1) => [t] -> t1<br>
><br>
> So how can I modify the function to have a type signature like the first<br>
> one?<br>
<br>
</div></div>You can't, since Num isn't a type, it's a typeclass.<br>
<br>
> myLength :: (Num b) => [a] -> b<br>
<br>
means that myLength takes a list of any type and can return any type<br>
that is an instance of Num (Num being the typeclass of numbers, that<br>
means that you can do most things you do on numbers, adding them,<br>
multiplying them, and so on...).<br>
<br>
If you want a simpler type signature, you could use :<br>
<br>
> myLength :: [a] -> Int<br>
<br>
or<br>
<br>
> myLength :: [a] -> Integer<br>
<br>
since Int (32 or 64 bits integer) and Integer are real type that are<br>
instances of the Num typeclass.<br>
<font color="#888888"><br>
--<br>
Jedaï<br>
</font></blockquote></div><br>