[Haskell-cafe] Signature of a function
sholderm at students.cs.uu.nl
Tue Jan 11 10:44:19 EST 2005
> Hint: Don't put signatures on functions, then. Instead, let the
> compiler infer the type for you! If you want to know what the type is,
> ask GHCi with :info. And if you think it is helpful documentation, you
> can copy-paste the correct signature from ghci into your source code!
Although in this specific case you might be right, I'm not too sure if
I agree with you. Especially for newbies, writing signatures upfront
can be good practice.
First, it lets you think about *what* you are going to write before you
actually write it. That way, it really forces structure on the way one
codes, and lack of structure is what usually makes learning a new
Second, it gives you a way to test whether what you write is what you
intended to write. Although the example is a bit contrived, I recently
saw a piece of code written by a relative newbie for selecting the last
element of a list:
last (x : xs : y) = y
If she had written a type signature for this function, the compiler
would have flagged this an error.
Admittedly, sometimes type signatures can get too intimidating for
novice Haskellers. Think about polymorphism and type classes. But,
well, one typically gets exposed to polymorphism quite soon, so that
should be doable, while it's possible to stay ignorant of type classes
for quite some while (if it weren't for those nasty error messages).
Just my two cents.
More information about the Haskell-Cafe