Statically typed programming languages would compile only if you specify types of the things that you have in your code. In functional programming world, you tend to have plenty of functions in various scopes abstracting away bits and pieces of logic. I strongly believe, the future is mostly functional and rarely object oriented for this very reason. After this, comes other reasons which I would save for a separate post.
Now that you have abstracted logic, you do get lazy to write the types of things. Type inference to the rescue! Quick definition from haskell wiki:
Type inference is a feature of the type system which means that concrete types are deduced by the type system where ever it is obvious. If you add an integer variable x to a numeric literal 2, then the type system concludes that 2, which in principle can represent 2 for every number type, must also be an integer, since + supports only addition of numbers of the same type.
Try this out in your ghci:
How did it work? Well, the type of the expression ‘addTwice’ got inferred by the haskell compiler:
Go ahead, experiment this one:
Beautiful the error message!. You can use this as a weapon to write neat code, and make it neater by refactoring at any time, without worrying too much about introducing accidents in your code.
I had all the types set, the return type of the function, the inputs and outputs of makeBits, everything; still the compiler failed to accept my code:
This fixed the problem:
Had it been haskell, I would have used ‘’ for the accumulator. It would have just worked. Another reason for me to vote for haskell!