0

I'm trying to learn Haskell in guidance of Learn You a Haskell, but the following puzzles me.

lucky :: (Integral a) => a -> String
lucky 7 = "LUCKY NUMBER SEVEN!"  
lucky x = "Sorry, you're out of luck, pal!"

As you can see, there's one line up there stating the exact types of the function. But is this necessary? Can't the types of parameters and return values be deduced from the patterns below that line?

1

2 Answers 2

6

You are right, they are absolutely not necessary. However, it's a very common practice to state the type of the function nevertheless, for at least two reasons :

  • To tell the compiler what you actually mean. In case you make a mistake writing the function, the compiler will not infer a bad type, but warn you of your mistake
  • To tell the people who read your code. They'll have to find out the type of the function anyway while understanding the code, so you might as well make it easier for them. Having the type explicitly makes the code more readable.

This is why, although they are optional, the types of top level functions are almost always spelled out in Haskell code.


To complete with what Zeta said, it is not necessary in this case. However, in some situations, it is necessary to specify the type of the function when the code is too ambiguous to infer.

Sign up to request clarification or add additional context in comments.

10 Comments

Does that line generate actual code (like some runtime type checking? I'm completely new to Haskell..)
you should read it as a hint to the compiler (which might or might not change the generated code based on those hints) - if you don't give it the compiler will try to infer it
"You are right, they are absolutely not necessary." In this case. There are several cases where GHC will yell at you if you leave the type signature and spit out several pages of ambiguity errors.
@JohnsonSteward: Nope! Haskell is statically typed, and all types are resolved at compile time. Whether you tell the compiler what the type is (with a type signature like that), or you let the compiler figure it out for itself (via type inference), the resulting generated code is the same. (One exception: if you give the code a more specific type, then the compiler may generate different output.)
Read further, and discovered this: zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]. Seems like this is not easily deducible for the compiler...
|
-2

For documentation purpose, and because for some type extensions the automatic inference fails. Read here.

4 Comments

1. Link is broken. 2. Currently, your answer is almost a link-only answer. Add more information into the answer itself.
@Zeta 2 agreed, 1 link is not broken though.
@JohnsonSteward: Ninja edit during the first five minutes.
Yes, I did a ninja edit after the first comment. By the way the I think the link has a good explaination for a haskell beginner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.