Why the length function I wrote has such a type signature?

This is exercise 3.1 of Real World Haskell. I have my length function like this: myLength [] = 0 myLength (_:xs) = 1 + (myLength xs) And I assumed the type signature is like this: mylength :: [a] -> Num But when I wrote this into the file and reloaded it into ghci, there is an error.
The type signature for `mylength' lacks an accompanying binding Failed, modules loaded: none.
And the type signature given by ghci is
myLength :: (Num t1) => [t] -> t1
So how can I modify the function to have a type signature like the first one?

On Thu, Nov 11, 2010 at 9:24 AM, 贾旭卿
This is exercise 3.1 of Real World Haskell. I have my length function like this:
myLength [] = 0 myLength (_:xs) = 1 + (myLength xs)
And I assumed the type signature is like this: mylength :: [a] -> Num
But when I wrote this into the file and reloaded it into ghci, there is an error.
The type signature for `mylength' lacks an accompanying binding Failed, modules loaded: none.
And the type signature given by ghci is
myLength :: (Num t1) => [t] -> t1
So how can I modify the function to have a type signature like the first one?
You can't, since Num isn't a type, it's a typeclass.
myLength :: (Num b) => [a] -> b
means that myLength takes a list of any type and can return any type that is an instance of Num (Num being the typeclass of numbers, that means that you can do most things you do on numbers, adding them, multiplying them, and so on...). If you want a simpler type signature, you could use :
myLength :: [a] -> Int
or
myLength :: [a] -> Integer
since Int (32 or 64 bits integer) and Integer are real type that are instances of the Num typeclass. -- Jedaï

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

On Thursday 11 November 2010 09:24:44, 贾旭卿 wrote:
This is exercise 3.1 of Real World Haskell. I have my length function like this:
myLength [] = 0 myLength (_:xs) = 1 + (myLength xs)
And I assumed the type signature is like this: mylength :: [a] -> Num
But when I wrote this into the file and reloaded it into ghci, there is an error.
The type signature for `mylength' lacks an accompanying binding Failed, modules loaded: none.
That's because you mistyped the function name in the type signature, the funcion has an uppercase L, the signature got a lowercase l. Without that typo, you'd have gotten an error: Class `Num' used as a type In the type signature for `myLength': myLength :: [a] -> Num which would probably have helped.
And the type signature given by ghci is
myLength :: (Num t1) => [t] -> t1
So how can I modify the function to have a type signature like the first one?
Jedaï answered that.

On 11/11/10 03:24, 贾旭卿 wrote:
This is exercise 3.1 of Real World Haskell. I have my length function like this:
myLength [] = 0 myLength (_:xs) = 1 + (myLength xs)
And I assumed the type signature is like this: mylength :: [a] -> Num
But when I wrote this into the file and reloaded it into ghci, there is an error.
The type signature for `mylength' lacks an accompanying binding Failed, modules loaded: none.
In addition to what Chaddaï Fouché said, this particular error is simply due to writing "mylength" when it should be "myLength" (L uppercase). -Isaac

Thanks everyone. I didn't even think about it.
2010/11/11 Isaac Dupree
On 11/11/10 03:24, 贾旭卿 wrote:
This is exercise 3.1 of Real World Haskell. I have my length function like this:
myLength [] = 0 myLength (_:xs) = 1 + (myLength xs)
And I assumed the type signature is like this: mylength :: [a] -> Num
But when I wrote this into the file and reloaded it into ghci, there is an error.
The type signature for `mylength' lacks an accompanying binding
Failed, modules loaded: none.
In addition to what Chaddaï Fouché said, this particular error is simply due to writing "mylength" when it should be "myLength" (L uppercase).
-Isaac _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

And the type signature given by ghci is
myLength :: (Num t1) => [t] -> t1
But why is the signature not written as: myLength :: [t] -> (Num t1) => t1 or something similar? I thought that the Num typeclass being first implied that it was the function's first argument.

On Friday 12 November 2010 20:14:25, Tom Murphy wrote:
And the type signature given by ghci is
myLength :: (Num t1) => [t] -> t1
But why is the signature not written as: myLength :: [t] -> (Num t1) => t1 or something similar?
I thought that the Num typeclass being first implied that it was the function's first argument.
A type signature like myLength's consists of two parts, - a context; here (Num t1) - the part giving the type (subject to the constraints in the context). The language definition says the context comes first, then "=>", finally the type. If contexts were always written next to the type they apply to, it would lead to unreadable type signatures and repetition (the same constraint can apply to several type variables). Not to mention multi parameter type classes. Strictly speaking, there's a third part, the quantification, but that's usually left implicit (explicit foralls require a language extension). So the full type is myLength :: forall a n. (Num n) => [a] -> n for all types a and n, if n is an instance of Num, myLength has (can have) the type [a] -> n.
participants (6)
-
amazingjxq
-
Chaddaï Fouché
-
Daniel Fischer
-
Isaac Dupree
-
Tom Murphy
-
贾旭卿