I am new to Haskell and find it hard learning type signatures. If I get a task that says "enter the type signatures of the following expressions", I am stuck. So, I was wondering if any of you guys knew a good way of learning this or if you have any websites/videos to recommend. I generally find it hard finding help or resources on the internet now that I've started my Haskell journey.
CodePudding user response:
If I get a task that says "enter the type signatures of the following expressions", I am stuck
Well, and that's a silly task too. For some reason Haskell courses keep asking it this way, but it's stupid. Inferring types is a task for a compiler (specifically its typechecker), or to put it another way for somebody learning type theory, but not for somebody learning Haskell.
Don't get me wrong, this is certainly something you will eventually be able to do easily, but it's not how you should in practice think about Haskell code. It almost always goes the other way around: you have some context, you see a gap an know what it's type is supposed to be, and then you find an expression that matches the type, not try to find the type of an expression. (Although, sometimes you do need to first find out the type of one expression in order to know what type the expression you need to write should have.)
However, what you absolutely need to learn is understanding type signatures. Practicing type inference by hand is one way of getting there, but more effective is to just both read an write lots of Haskell code. Doesn't really matter what the code does, just pick any project you find interesting and look at its Haddock documentation (usually easiest on Hackage). Pick any function, and try to understand why the given description corresponds to the specified type. Then look at the source and trace the types through the implementation.
Also, just toy around a lot with GHCi, think of simple problems you'd like to solve. Perhaps contests like Project Euler, but I never did that personally.