Getting started with Haskell
Asked Answered
S

15

754

For a few days I've tried to wrap my head around the functional programming paradigm in Haskell. I've done this by reading tutorials and watching screencasts, but nothing really seems to stick. Now, in learning various imperative/OO languages (like C, Java, PHP), exercises have been a good way for me to go. But since I don't really know what Haskell is capable of and because there are many new concepts to utilize, I haven't known where to start.

So, how did you learn Haskell? What made you really "break the ice"? Also, any good ideas for beginning exercises?

Sulcus answered 18/6, 2009 at 13:17 Comment(1)
tryhaskell.orgVasty
G
2576

I'm going to order this guide by the level of skill you have in Haskell, going from an absolute beginner right up to an expert. Note that this process will take many months (years?), so it is rather long.

Absolute Beginner

Firstly, Haskell is capable of anything, with enough skill. It is very fast (behind only C and C++ in my experience), and can be used for anything from simulations to servers, guis and web applications.

However there are some problems that are easier to write for a beginner in Haskell than others. Mathematical problems and list process programs are good candidates for this, as they only require the most basic of Haskell knowledge to be able to write.

Some good guides to learning the very basics of Haskell are the Happy Learn Haskell Tutorial and the first 6 chapters of Learn You a Haskell for Great Good (or its JupyterLab adaptation). While reading these, it is a very good idea to also be solving simple problems with what you know.

Another two good resources are Haskell Programming from first principles, and Programming in Haskell. They both come with exercises for each chapter, so you have small simple problems matching what you learned on the last few pages.

A good list of problems to try is the haskell 99 problems page. These start off very basic, and get more difficult as you go on. It is very good practice doing a lot of those, as they let you practice your skills in recursion and higher order functions. I would recommend skipping any problems that require randomness as that is a bit more difficult in Haskell. Check this SO question in case you want to test your solutions with QuickCheck (see Intermediate below).

Once you have done a few of those, you could move on to doing a few of the Project Euler problems. These are sorted by how many people have completed them, which is a fairly good indication of difficulty. These test your logic and Haskell more than the previous problems, but you should still be able to do the first few. A big advantage Haskell has with these problems is Integers aren't limited in size. To complete some of these problems, it will be useful to have read chapters 7 and 8 of learn you a Haskell as well.

Beginner

After that you should have a fairly good handle on recursion and higher order functions, so it would be a good time to start doing some more real world problems. A very good place to start is Real World Haskell (online book, you can also purchase a hard copy). I found the first few chapters introduced too much too quickly for someone who has never done functional programming/used recursion before. However with the practice you would have had from doing the previous problems you should find it perfectly understandable.

Working through the problems in the book is a great way of learning how to manage abstractions and building reusable components in Haskell. This is vital for people used to object-orientated (oo) programming, as the normal oo abstraction methods (oo classes) don't appear in Haskell (Haskell has type classes, but they are very different to oo classes, more like oo interfaces). I don't think it is a good idea to skip chapters, as each introduces a lot new ideas that are used in later chapters.

After a while you will get to chapter 14, the dreaded monads chapter (dum dum dummmm). Almost everyone who learns Haskell has trouble understanding monads, due to how abstract the concept is. I can't think of any concept in another language that is as abstract as monads are in functional programming. Monads allows many ideas (such as IO operations, computations that might fail, parsing,...) to be unified under one idea. So don't feel discouraged if after reading the monads chapter you don't really understand them. I found it useful to read many different explanations of monads; each one gives a new perspective on the problem. Here is a very good list of monad tutorials. I highly recommend the All About Monads, but the others are also good.

Also, it takes a while for the concepts to truly sink in. This comes through use, but also through time. I find that sometimes sleeping on a problem helps more than anything else! Eventually, the idea will click, and you will wonder why you struggled to understand a concept that in reality is incredibly simple. It is awesome when this happens, and when it does, you might find Haskell to be your favorite imperative programming language :)

To make sure that you are understanding Haskell type system perfectly, you should try to solve 20 intermediate haskell exercises. Those exercises using fun names of functions like "furry" and "banana" and helps you to have a good understanding of some basic functional programming concepts if you don't have them already. Nice way to spend your evening with a bunch of papers covered with arrows, unicorns, sausages and furry bananas.

Intermediate

Once you understand Monads, I think you have made the transition from a beginner Haskell programmer to an intermediate haskeller. So where to go from here? The first thing I would recommend (if you haven't already learnt them from learning monads) is the various types of monads, such as Reader, Writer and State. Again, Real world Haskell and All about monads gives great coverage of this. To complete your monad training learning about monad transformers is a must. These let you combine different types of Monads (such as a Reader and State monad) into one. This may seem useless to begin with, but after using them for a while you will wonder how you lived without them.

Now you can finish the real world Haskell book if you want. Skipping chapters now doesn't really matter, as long as you have monads down pat. Just choose what you are interested in.

With the knowledge you would have now, you should be able to use most of the packages on cabal (well the documented ones at least...), as well as most of the libraries that come with Haskell. A list of interesting libraries to try would be:

  • Parsec: for parsing programs and text. Much better than using regexps. Excellent documentation, also has a real world Haskell chapter.

  • QuickCheck: A very cool testing program. What you do is write a predicate that should always be true (eg length (reverse lst) == length lst). You then pass the predicate the QuickCheck, and it will generate a lot of random values (in this case lists) and test that the predicate is true for all results. See also the online manual.

  • HUnit: Unit testing in Haskell.

  • gtk2hs: The most popular gui framework for Haskell, lets you write gtk applications.

  • happstack: A web development framework for Haskell. Doesn't use databases, instead a data type store. Pretty good docs (other popular frameworks would be snap and yesod).

Also, there are many concepts (like the Monad concept) that you should eventually learn. This will be easier than learning Monads the first time, as your brain will be used to dealing with the level of abstraction involved. A very good overview for learning about these high level concepts and how they fit together is the Typeclassopedia.

  • Applicative: An interface like Monads, but less powerful. Every Monad is Applicative, but not vice versa. This is useful as there are some types that are Applicative but are not Monads. Also, code written using the Applicative functions is often more composable than writing the equivalent code using the Monad functions. See Functors, Applicative Functors and Monoids from the learn you a haskell guide.

  • Foldable,Traversable: Typeclasses that abstract many of the operations of lists, so that the same functions can be applied to other container types. See also the haskell wiki explanation.

  • Monoid: A Monoid is a type that has a zero (or mempty) value, and an operation, notated <> that joins two Monoids together, such that x <> mempty = mempty <> x = x and x <> (y <> z) = (x <> y) <> z. These are called identity and associativity laws. Many types are Monoids, such as numbers, with mempty = 0 and <> = +. This is useful in many situations.

  • Arrows: Arrows are a way of representing computations that take an input and return an output. A function is the most basic type of arrow, but there are many other types. The library also has many very useful functions for manipulating arrows - they are very useful even if only used with plain old Haskell functions.

  • Arrays: the various mutable/immutable arrays in Haskell.

  • ST Monad: lets you write code with a mutable state that runs very quickly, while still remaining pure outside the monad. See the link for more details.

  • FRP: Functional Reactive Programming, a new, experimental way of writing code that handles events, triggers, inputs and outputs (such as a gui). I don't know much about this though. Paul Hudak's talk about yampa is a good start.

There are a lot of new language features you should have a look at. I'll just list them, you can find lots of info about them from google, the haskell wikibook, the haskellwiki.org site and ghc documentation.

  • Multiparameter type classes/functional dependencies
  • Type families
  • Existentially quantified types
  • Phantom types
  • GADTS
  • others...

A lot of Haskell is based around category theory, so you may want to look into that. A good starting point is Category Theory for Computer Scientist. If you don't want to buy the book, the author's related article is also excellent.

Finally you will want to learn more about the various Haskell tools. These include:

  • ghc (and all its features)
  • cabal: the Haskell package system
  • darcs: a distributed version control system written in Haskell, very popular for Haskell programs.
  • haddock: a Haskell automatic documentation generator

While learning all these new libraries and concepts, it is very useful to be writing a moderate-sized project in Haskell. It can be anything (e.g. a small game, data analyser, website, compiler). Working on this will allow you to apply many of the things you are now learning. You stay at this level for ages (this is where I'm at).

Expert

It will take you years to get to this stage (hello from 2009!), but from here I'm guessing you start writing phd papers, new ghc extensions, and coming up with new abstractions.

Getting Help

Finally, while at any stage of learning, there are multiple places for getting information. These are:

  • the #haskell irc channel
  • the mailing lists. These are worth signing up for just to read the discussions that take place - some are very interesting.
  • other places listed on the haskell.org home page

Conclusion

Well this turned out longer than I expected... Anyway, I think it is a very good idea to become proficient in Haskell. It takes a long time, but that is mainly because you are learning a completely new way of thinking by doing so. It is not like learning Ruby after learning Java, but like learning Java after learning C. Also, I am finding that my object-orientated programming skills have improved as a result of learning Haskell, as I am seeing many new ways of abstracting ideas.

Galba answered 18/6, 2009 at 13:17 Comment(22)
Thank you for the thorough answer. At my current stage (absolute beginner) I am especially pleased to know about the Haskell 99 problems. I also found this thread #780300 which I think is probably relevant to programmers before or at the "Absolute beginner" stage.Jampack
Yay arrows! First you let monads shape your brain, then you stand on your head and think about comonads, and then you do both at the same time to get arrows :) There's a lot of expressive power in Haskell that can be opened up with type-level programming, too.Subaudition
@nanothief: Learning about Monad before Functor and Applicative is an unnatural progression... better to learn them in the right order, as laid out by the typeclassopediaPresbyter
@pelotom: In the real world haskell book, Functors are introduced before Monads (chapter 10 vs chapter 14). so if you read it in order you will already know Functors. Not sure if it is worth removing Functors from the list of things to learn after monads because of this, seems like a minor issue. With regards to Applicative, I don't think it is that important. Almost all types that can be instances of Applicative can also be instances of Monad. As Monad is more powerful, it is a much more useful concept to know. It also isn't hard to learn Applicative after Monad if you do need it.Galba
@nanothief Monad is more powerful, but also less compositional... a lot of people use monads where they could've gotten away with cleaner Applicative code. Most things that are Functors are also Monads, but you don't go around using >>= and return when fmap will suffice, because the latter leads to much simpler code if you can use it.Presbyter
@nanothief My point being that it makes sense to learn them in ascending order of power, which is also ascending order of complexity, and also learn to use the least complex tool which will get the job done.Presbyter
@pelotom, I've added the typeclassopedia link as well as a better reasons to use Applicative to that section, and removed the Functor section. It is hard getting the Monad and Applicative concepts in the right order since of the emphasis on Monads in most teaching material (including RWH). On the other hand, the teach you a haskell tutorial has come a long way since I initially wrote the answer (nearly 2 years :O), and does teach Applicative before Monad, perhaps that should now be the recommended way of learning haskell.Galba
Nishant Shukla's lectures is a pretty good starting point to the absolute beginner. They offer a nice introduction to Haskell.Copartner
Great advice. I started this over a year ago, and am most of the way through the Intermediate stage. Feedback: RWH's monad chapter (chapter 14) is poorly explained. Reading the online version of RWH is beneficial, because it contains crowdsourced comments which aid the chapter. FWIW, You Could Have Invented Monads, was the monad tutorial that worked best for me.Pulpwood
@tomf: Thanks! I've always been amazed at how well this answer has done - it has nearly been five years since I wrote it but it is still going strong. I will need to do an update to it soon though, as it is a bit out of date. It doesn't mention lenses, pipes, constraint kinds, the haskell platform, type level numbers, and they are pretty major new topics since this was written. You are right that RWH isn't as good anymore, it hasn't been updated in a long time and a lot of the examples aren't compiling. I'm glad it was still helpful to you anyway.Galba
Why do you only recommend the first 6 chapters of learnyouahaskell?? So you do not recommend the further with typeclasses, modules, streams,etc. for a beginner? I think one should read the whole tutorial.Towhee
@Timo: the problem I had with learn you a haskell was it didn't have many practical examples - while it taught the principals well, you never really got around to writing an actual useful program. RWH didn't have this problem, but was a bit too dense for a new programmer. So starting with LYAH and moving to RWH seemed like the best compromise without doubling up on too much. Both are excellent resources though so stick with whatever works for you.Galba
You know an introduction for people who already know the math? I'd imagine that would start with Hask and just explain how e.g. product and sum-types are implemented etc. I'm reading into Haskell on and off now for a year, have a relatively good understanding of Monads etc, and the basic features of it. The irc channel is very good. But if I really want to code I need some syntax specific to Haskell and then it seems I'm forced to read books such as "Real World Haskell" or "Learn Yourself a Haskell", where they avoid good straight forward abstractions for 150 pages to not scare anybody off.Michellmichella
Hey, I think we should add this book also by Chris Allen Haskell Programming from first Principles. It is also a good book.Grosgrain
-1 for recommending lyah, which is one of the worst resources out there: too verbose, regularly confusing beginners, and no exercises whatsoever. Realworldhaskell is extremely outdated as well. Common consensus in the #haskell channel is this link github.com/bitemyapp/learnhaskellHemline
@hasufell: Yes this question is significantly out of date (it was written in 2009, and not chanced much since). RWH very out of date, as it was written before pipes/lens/stack were a thing. LYAH does have its flaws, but at the time I felt it was the best simple introduction to haskell freely available. Unfortunately it seems the haskell learning resources have deteriorated since 2009 then, as there is nothing like RHW freely available now. The course recommended in the learnhaskell github link are okay, but you don't seem to do anything practical (apart from parsing) in the entire course.Galba
I found something likes a roadmap here in case all of you need: lambdaconf.us/downloads/documents/lambdaconf_slfp.pdfEffect
How should one proceed when wanting to learn Haskell and parallel processing.Are there any resources for learning Haskell for WebSocket designed Web Api-s?Athwart
Think the original question and replies are very important to anyone learning Haskell. I'm a beginner too. Started out with several books, and got stuck in a 'tutorial-loop', mostly because I like the learning process. Although I soon enough discovered that getting my feet wet in starting writing my own simple programs worked best. Bumped my head several times but by posting questions here, the answers helped me along and taught me in the same process.Decommission
cis194/spring13 (seas.upenn.edu/~cis194/spring13/lectures.html) is a great place to start. Then (fp-course) github.com/bitemyapp/fp-course is also good for beginners.Reversible
I'm reading this in 2021 as I try as a pre-cursor to learning Plutus. And I find this still very valuable!Salomie
>It will take you years to get to this stage (hello from 2009!), but from here I'm guessing you start writing phd papers, new ghc extensions, and coming up with new abstractions. Experts do stuff. Not talk about doing stuff. For that reason I would be hesitant call an expert somebody that has never deployed anything to production environment.Hutcherson
A
186

Some colleague of mine had good experience with Learn You a Haskell for Great Good!.

Tutorial aimed at people who have experience in imperative programming languages but haven't programmed in a functional language before.

And check the answers here too

Adamski answered 18/6, 2009 at 13:17 Comment(7)
I second this. Also, since it's not obvious, here's a link to a downloadable pdf version of the tutorial: learnyouahaskell.com/learnyouahaskell.pdf The webdesign is great, but I like to have a copy for the subway too.Colcothar
I started with this, but my opinion is that you should go directly to Real World Haskell. The difference is like learning C from K&R or "C for dummies" which tries to be simple, but misses important stuff with its approach. I think it's better to just get the facts straight instead of trying to learn Haskell "the imperative way".Arraignment
I absolutely LOVE this, and I have invested a lot of time into this and Real World Haskell. IMO, "Learn You a Haskell" gives deeper insight than Real World Haskell, though they're both great resources.Nichellenichol
I've just finished reading LYAHFGG cover to cover - super-highly recommended.Pongee
@abababa22 I think reading LYAH first and then going to RWH is the best idea. LYAH doesn't teach you just Haskell; it teaches you functional programming. You begin thinking in functional way when you solve problems. Clearly, only LYAH wouldn't be enough to write a large application, but it bends your mind the right way. If you're from imperative background, this is the best way, IMOLoser
@Colcothar Just to note: the PDF is not the final version, at least it is missing the last chapter.Apocrine
@Apocrine That makes sense in a way. The book is now officially released and for sale from NoStarch Press. (Link: nostarch.com/lyah.htm) So the online version may be incomplete.Colcothar
P
104

Here's a good book that you can read online: Real World Haskell

Most of the Haskell programs I've done have been to solve Project Euler problems.

Once piece of advice I read not too long ago was that you should have a standard set of simple problems you know how to solve (in theory) and then whenever you try to learn a new language you implement those problems in that language.

Preciosa answered 18/6, 2009 at 13:17 Comment(3)
Real World Haskell in my experience is great, until you reach chapter 5. From then on I wouldn't recommend it.Tum
Why @MasterMastic? What is the problem beyond chapter 5? I'd like to know before I spend the money.Valma
@JayBlanchard In chapter 5 you start getting a concrete example of a library, which is nice, but they tell you what they're going to do, do it, but they do not explain why entirely, and not clearly at all, and there's quite a bit of magic hex literals. You're just going through the motions. That wasn't the biggest issue for me tho, the biggest issue was that the book heavily depends on those kind of hard and long examples (long enough to take more than an entire chapter). You can hardly just read the parts you want to. I think great authors, amazing knowledge but extremely poor execution.Tum
P
73

I enjoyed watching this 13 episode series on Functional Programming using Haskell.

C9 Lectures: Dr. Erik Meijer - Functional Programming Fundamentals: http://channel9.msdn.com/shows/Going+Deep/Lecture-Series-Erik-Meijer-Functional-Programming-Fundamentals-Chapter-1/

Pee answered 18/6, 2009 at 13:17 Comment(0)
R
71

To add on others' answers - there is one useful that will help you when coding (for example when solving project Euler problems): Hoogle. You can use either the command line interface or the web interface.

Command Line

After you installed the Haskell platform be sure to cabal install hoogle

Hoogle usage example:

You have a function f x = 3 * x + 1 and you want to apply it on (5 :: Int), then apply it on the result and on that result and so on and get an infinite list of those values. You suspect there might already exist a function to assist you (not specifically for your f though).

That function would be of type (a -> a) -> a -> [a] if it takes f 5 or a -> (a -> a) -> [a] if it takes 5 f (we assume the function is for general types and not just Ints)

$ hoogle "a -> (a -> a) -> [a]"
Prelude iterate :: (a -> a) -> a -> [a]

yep, the function you need already exists and it's called iterate. you use it by iterate func 5!

Web interface

The result for the same example can be found here.

Reisch answered 18/6, 2009 at 13:17 Comment(1)
Finding the standard library functions for what you need gets so much easier once you understand how to ask Hoogle for what you need.Insignia
B
60

Graham Hutton's Programming in Haskell is concise, reasonably thorough, and his years of teaching Haskell really show. It's almost always what I recommend people start with, regardless of where you go from there.

In particular, Chapter 8 ("Functional Parsers") provides the real groundwork you need to start dealing with monads, and I think is by far the best place to start, followed by All About Monads. (With regard to that chapter, though, do note the errata from the web site, however: you can't use the do form without some special help. You might want to learn about typeclasses first and solve that problem on your own.)

This is rarely emphasized to Haskell beginners, but it's worth learning fairly early on not just about using monads, but about constructing your own. It's not hard, and customized ones can make a number of tasks rather more simple.

Brathwaite answered 18/6, 2009 at 13:17 Comment(1)
This is a totally under-appreciated book (and answer). The chapter on functional parsers, followed by a chapter on IO, neither of which even mention monads, really shines as an elegant pedagogical approach.Logician
A
56

Don't try to read all the monad tutorials with funny metaphors. They will just get you mixed up even worse.

Arraignment answered 18/6, 2009 at 13:17 Comment(2)
Agreed! See 'Abstraction, intuition, and the “monad tutorial fallacy”': byorgey.wordpress.com/2009/01/12/…Frivolity
I also agreed! However, if you wanna give a try, this video is a really a nice first step. It approaches monads regarding Java programming that usually makes people more comfortable.Fallon
A
31

I'd suggest joining the #haskell irc channel and asking questions there. That's how I learned Haskell. If you go through Real World Haskell as suggested above, real time answers to your questions will help greatly. Lots of smart people on #haskell write Haskell for fun and for profit, so you'll get lots of good input. Try it!

Affirmative answered 18/6, 2009 at 13:17 Comment(2)
+1 - To be clear: Do not learn it just with the irc channel. As in, don't go in and ask "How do I write a haskell program? How do I add numbers?"Alemanni
In addition to irc freenode, there is also lately a growing lively discussion on haskell in Discord chats.Knuth
R
23

These are my favorite

Haskell: Functional Programming with Types

Joeri van Eekelen, et al. | Wikibooks
       Published in 2012, 597 pages

Real World Haskell

   B. O'Sullivan, J. Goerzen, D. Stewart | OReilly Media, Inc.
   Published in 2008, 710 pages
Romito answered 18/6, 2009 at 13:17 Comment(0)
O
19

I can additionally recommend Yet Another Haskell Tutorial as an introduction.

Another good learning resource (probably on the intermediate level), which has helped me a lot and hasn't been mentioned in the other answers as far as I can see, is Brent Yorgey's Typeclassopedia, which can be found in The Monad Reader (Issue 13)

It is written in a very accessible style and contains (among many other things), the following introductory advice:

There are two keys to an expert Haskell hacker’s wisdom:

  1. Understand the types.

  2. Gain a deep intuition for each type class and its relationship to other type classes, backed up by familiarity with many examples.

The Monad Reader itself is an absolute treasure trove for functional programmers (not only Haskell programmers).

Oldtime answered 18/6, 2009 at 13:17 Comment(0)
H
16

Try writing easy programs in it.

You can find sample tasks in various textbooks, probably.

I wouldn't recommend sticking to Haskell/FP textbooks, just try to do simple things with it: calculations, string manipulations, file access.

After I solved a dozen, I've broke the ice :)

After that, read a lot on advanced concepts (Monads, Arrows, IO, recursive data structures), because haskell is infinite and there are a lot of them.

Hangnail answered 18/6, 2009 at 13:17 Comment(0)
C
14

I do think that realizing Haskell's feature by examples is the best way to start above all.

http://en.wikipedia.org/wiki/Haskell_98_features

Here is tricky typeclasses including monads and arrows

http://www.haskell.org/haskellwiki/Typeclassopedia

for real world problems and bigger project, remember these tags: GHC(most used compiler), Hackage(libraryDB), Cabal(building system), darcs(another building system).

A integrated system can save your time: http://hackage.haskell.org/platform/

the package database for this system: http://hackage.haskell.org/

GHC compiler's wiki: http://www.haskell.org/haskellwiki/GHC

After Haskell_98_features and Typeclassopedia, I think you already can find and read the documention about them yourself

By the way, you may want to test some GHC's languages extension which may be a part of haskell standard in the future.

this is my best way for learning haskell. i hope it can help you.

Cavendish answered 18/6, 2009 at 13:17 Comment(0)
N
13

If you only have experience with imperative/OO languages, I suggest using a more conventional functional language as a stepping stone. Haskell is really different and you have to understand a lot of different concepts to get anywhere. I suggest tackling a ML-style language (like e.g. F#) first.

Noonday answered 18/6, 2009 at 13:17 Comment(2)
Elm might be the closest, more usefule and beginner-friendly of these alternatives...Felspar
I disagree on passing by thru a temporary route, like F#. For me, it's like drinking a vodka you have to sip it in quickly. More painful that way, but pleasure is also there. Temporary routes, only leads to more confusion for me.Knuth
J
13

I suggest that you first start by reading BONUS' tutorial, And then reading Real World Haskell (online for free). Join the #Haskell IRC channel, on irc.freenode.com, and ask questions. These people are absolutely newbie friendly, and have helped me a lot over time. Also, right here on SO is a great place to get help with things you can't grasp! Try not to get discouraged, once it clicks, your mind will be blown.

BONUS' tutorial will prime you up, and get you ready for the thrill ride that Real World Haskell brings. I wish you luck!

Jetliner answered 18/6, 2009 at 13:17 Comment(0)
D
11

The first answer is a very good one. In order to get to the Expert level, you should do a PhD with some of the Experts themselves.

I suggest you to visit the Haskell page: http://haskell.org. There you have a lot of material, and a lot of references to the most up-to-date stuff in Haskell, approved by the Haskell community.

Disgust answered 18/6, 2009 at 13:17 Comment(1)
Sorry but using the PhD argument here is like saying you have to have a 300$ kitchen knife to be a good chef. Even Simon Peyton Jones - the father of Haskell - does not have a PhD. Practice and persistence is what leads to expertise both here and in any other field.Attrahent

© 2022 - 2024 — McMap. All rights reserved.