I'm thinking of learning Haskell, so I was wondering what issues are often ran into by Haskell newbies (especially ones which haven't had much experience with functional programming). Any other kind of advice for a programmer new to Haskell would be very much appreciated as well.
If you start everything with do
, you're DOing it wrong :)
Seriously, the concept of functional programming is quite different from imperative programming.
The Wikipedia page has some very basic, but good examples to get you started on the concept. Once you're used to that, Haskell is quite nice. No really dangerous pitfalls that I can remember.
...well, I do remember finding it terribly annoying that on each course that used Haskell, that teacher had his own parser libraries for generating parse trees. Of course, they all claimed theirs was better than the other teachers'. In truth, Erik Meijer's kicked ass :) If you don't feel like reading, do click that link and watch the video.
look into the following things:
- recursion
- pattern matching
- monads :) and start of with the IO monad :)
You can't use imperative style programming so forget about loops and if-then-else routines ;)
oh and most importantly ... buy a good book like 'Real world Haskell' from o'Reilly
I can give you my advice as a recent Haskell noob. I'm still very new to Haskell, but I've found the language much more comfortable than I thought I would. Here's what I would suggest:
- Get yourself at least three good tutorials. Real World Haskell is great, but I needed YAHT(pdf) and the Wikibook to double-check some topics when I started out.
- Download GHC and get comfortable with it.
- Find some exercises and do them. I worked through every exercise in Chapters 1-3 of Real World Haskell and it made a huge difference.
- Don't panic. While working through the aforementioned exercises, I often found myself tearing my hair out over small typing errors. You will eventually learn to love the type system, but not before you learn to hate it.
- Speaking of typing, as soon as you're able, make sure you explicitly type your functions. Don't rely on the compiler to infer types for you -- that causes subtle bugs that will drive you crazy.
- Don't worry about monads initially. In fact, until you understand a bit about typeclasses, forget you even heard the word "monad."
Have fun! Haskell is a very unique language, but the community is still small and very welcoming.
EDIT: Also, for a fantastic discussion about the learning curve, check out this question.
In no particular order:
The book "Real world Haskell", as stated previously, is a fantastic learning tool. You can read it free online, but I do recommend the print copy.
Once you get comfortable with lists (and you will, quickly), favor the foldr function over foldl to avoid "space leaking".
Get used to the idea that you are learning something new from scratch. Your imperative language background does not apply.
The "return" keyword doesn't do what you think it does. Avoid using it at all while starting out.
Favor the standard library.
Look for an algorithm you can use within the standard library before implementing one.
Chances are someone else is doing something similar to what your doing. Look for packages you can use (http://hackage.haskell.org/packages/archive/pkg-list.html) before implementing a new one.
Most importantly, HAVE FUN! Don't let learning Haskell become a chore.
The concept of monads will be confusing because they are typically explained with their mathematical definition. This is socially very incompetent behavior because the definition is the most complicated form to explain their usefulness.
I will explain the IO-monad right now for you in concrete terms. It corresponds to the following C# code:
interface IOMonad {} //just a marker interface - has no meaning
class WriteLine : IOMonad
{
string Text;
Func<IOMonad> ContinueWith;
}
class ReadLine : IOMonad
{
Func<string, IOMonad> ContinueWith;
}
static IOMonad Main()
{
return new WriteLine()
{
Text = "Your Name?",
ContinueWith = new ReadLine()
{
ContinueWith = inputText => new WriteLine() { Text = "You are " + inputText }
}
};
}
Can you guess the programs behavior? When executed, the side effects of printing to the console are not executed by your main function but are returned as a data structure. The runtime system outside of main will now interpret your commands. Notice that we did not do any side effects. We could call Main 0, 1 or N times without making any difference.
The ContinueWith-part of the code is called the bind operator (do-syntax or >>= operator). it tells the monad to do "something" and continue with the function you provided. this function can respond to the console input for example. it returns the next IO-action to execute.
The return operator would be class NoOp : IOMonad { object Value; }
.
This was the most complicated monad to understand i know of.
In addition to “Real World Haskell” book you can look on this one: “Learn You a Haskell for Great Good”