[Haskell-beginners] Why the length function I wrote has such a type signature?

Daniel Fischer daniel.is.fischer at web.de
Thu Nov 11 04:04:58 EST 2010

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.

More information about the Beginners mailing list