[Haskell-cafe] I love type declarations. :-)

Sebastian Sylvan sebastian.sylvan at gmail.com
Fri May 6 10:51:27 EDT 2005


On 5/6/05, Daniel Carrera <dcarrera at digitaldistribution.com> wrote:
> Hey,
> 
> Whooo hoo! I just finished my first non-trivial Haskell program. It's
> the pseudo-random number generator from RC4. And if it weren't for type
> declarations I'd still be bug-hunting.
> 
> I've been trying to get the PRNG to work for two days, but Hugs kept
> wanting to use Integer where Int should be used, ugh. But while reading
> a Haskell tutorial I came accross type declarations:
> 
> function ::  Int -> Int -> [Int]
> 
> This solved all my problems! I added proper type declarations to
> everything, and suddenly Hugs was happy. Type declarations also brought
> other benefits:
> 
> 1) I quickly spoted two bugs I hadn't hit yet.
> 2) I removed the fromIntegral() functions, making the code cleaner.
> 3) And my code is self-documented.
> 
> Before, when writing imperative code, I would add a comment for each
> function describing its input and output. Now type declarations provide
> that, so I merely describe what the function does. And type declarations
> are better than a comment because Haskell helps me make sure that they
> are right.
> 
> Go Haskell!
> 

Indeed, type annotations are very useful, at least for the "top-most"
functions in your module (the rest of the functions are usually so
simple that there's no need for them from a documentation standpoint,
and if the top-most functions have correct types the compiler can
usually infer the types of the other functions easily).

/S

-- 
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862


More information about the Haskell-Cafe mailing list