This is my recommended path for learning Haskell.
Something to keep in mind: don't sweat the stuff you don't understand immediately. Just keep moving.
Our IRC channel is #haskell-beginners
on Freenode.
IRC web client here: http://webchat.freenode.net/
Letter to a Young Haskell Enthusiast
Be nice above all else!
Haskell is a programming language as laid out in the reports, most recent one being in 2010. http://www.haskell.org/onlinereport/haskell2010/
GHC is the most popular compiler for Haskell and is what you'll install along with Cabal. Cabal is the project and dependency management tool used with GHC. You almost definitely want both if you're going to start writing Haskell.
Cabal is equivalent to Ruby's Bundler, Python's pip, Node's NPM, Maven, etc. GHC manages packaging itself, Cabal chooses what versions to install.
This PPA is excellent and is what I use on all my Linux dev and build machines: http://launchpad.net/~hvr/+archive/ghc
Specifically:
sudo apt-get update
- 12.04 and below ->
sudo apt-get install python-software-properties
- 12.04 and above ->
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:hvr/ghc
sudo apt-get update
sudo apt-get install cabal-install-1.20 ghc-7.8.3 happy-1.19.4 alex-3.1.3
Then add ~/.cabal/bin:/opt/cabal/1.20/bin:/opt/ghc/7.8.3/bin:/opt/happy/1.19.4/bin:/opt/alex/3.1.3/bin
to your PATH (bash_profile, zshrc, bashrc, etc)
Optional You could also add .cabal-sandbox/bin
to your path. Code that you are actively developing will be available to you from the command line.
This only works when your current working directory is a cabal sandbox.
If you use Debian stable, it is easier to use http://deb.haskell.org/. To use this:
- Add the line
deb http://deb.haskell.org/stable/ ./
to/etc/apt/sources.list
- run
sudo apt-get update
- run
sudo apt-get install ghc-7.8.3 happy alex cabal-install
If you're not using stable, you can follow the same steps as Ubuntu, but has to execute an additional command. Immediately after sudo add-apt-repository -y ppa:hvr/ghc
is ran, run:
sudo sed -i s/jessie/trusty/g /etc/apt/sources.list.d/hvr-ghc-jessie.list
For other Debian versions, just replace all occurences of "jessie" with your version name in the command above.
If, for some reason, the file /etc/apt/sources.list.d/hvr-ghc-jessie.list
does
not exist, then /etc/apt/sources.list
should contain a line like this:
deb http://ppa.launchpad.net/hvr/ghc/ubuntu jessie main
Replace "jessie" with "trusty" in this line.
You can follow the guide written for Mac OS X: http://www.davesquared.net/2014/05/platformless-haskell.html. Notes:
- set your prefix accordingly when configuring ghc
- instead of grabbing the
cabal-install
binary, grab the source and then runbootstrap.sh
script.
To install Haskell 7.8x from the unofficial repo (Fedora 21+ will include it in the official one:
- Add https://copr.fedoraproject.org/coprs/petersen/ghc-7.8/repo/fedora-20/petersen-ghc-7.8-fedora-20.repo as petersen-ghc-7.8-fedora-20.repo
sudo yum install ghc
To install Haskell from the official repos on Arch Linux, run
su -c "pacman -S cabal-install ghc happy alex haddock"
On Gentoo, you can install the individual components of the Haskell Platform
through Portage. If you use ACCEPT_KEYWORDS=arch
(as opposed to
ACCEPT_KEYWORDS=~arch
), Portage will install ancient versions of the various
Haskell things. With that in mind, iff you use ACCEPT_KEYWORDS=arch
, add the
following to /etc/portage/package.keywords
.
dev-haskell/cabal-install
dev-lang/ghc
Once that is done,
emerge -jav dev-lang/ghc dev-haskell/cabal-install
Gentoo keeps a "stable" (read: old) version of cabal-install
in the Portage
tree, so you'll want to use cabal-install
to install the more recent
version. Note that the backslashes are intentional.
\cabal update
\cabal install cabal-install
You have now installed cabal on a global scale with portage, and locally in your
home directory with cabal-install
. The next step is to make sure that when you
run cabal
in a terminal, your shell will run the up-to-date version in your
home directory. You will want to add the following lines to your shell's
configuration file:
PATH=$PATH:$HOME/.cabal/bin
alias cabal="$HOME/.cabal/bin/cabal"
If you don't know what your shell is, more than likely, your shell is Bash. If
you use Bash, the file you will edit is ~/.bashrc
. If you use Z-shell, the
file is ~/.zshrc
. You can run the following command to find out what your
shell is.
< /etc/passwd grep $(whoami) | awk -F/ '{print $NF}'
I use zsh, so that command outputs zsh
when I run it.
Once you do all of that, you'll want to install the additional tools alex
and
happy
.
cabal install alex happy
Congratulations! You now have a working Haskell installation!
Install the GHC for Mac OS X app, which includes GHC and Cabal. It provides instructions on how to add GHC and Cabal to your path after you've dropped the .app somewhere.
Do the binary distribution install described below with this tarball:
- http://neilmitchell.blogspot.com/2014/12/beta-testing-windows-minimal-ghc.html is able to compile
network
et al. Technically in beta but should work for the purposes of anybody reading this guide.
Don't forget to run the installer as administrator as it will want to install in your Program Files.
Download the latest binary distributions for cabal and ghc:
GHC is the most popular way to work in the Haskell language. It includes a compiler, REPL (interpreter), package management, and other things besides.
Cabal does project management and dependency resolution. It's how you'll install projects, typically into their own sandbox.
You don't need this if you use the .app, but if it doesn't work for you, try this with the binary distribution.
http://www.seas.upenn.edu/~cis194/spring13/index.html Brent Yorgey's course is the best I've found so far and replaces both Yann Esposito's HF&H. This course is valuable as it will not only equip you to write basic Haskell but also help you to understand parser combinators.
The only reason you shouldn't start with cis194 is if you are not a programmer or are an inexperienced one. If that's the case, start with http://learnyouahaskell.com/ and transition to cis194.
If you are not new to programming, Learn You A Haskell and http://book.realworldhaskell.org/ are recommended primarily as supplemental references for completing the cis194 course. RWH has some additional material that LYAH does not that is useful to people using Haskell in production as well.
This is Bryan O'Sullivan's online course from the class he teaches at Stanford. If you don't know who he is, take a gander at half the libraries any Haskell application ends up needing and his name is on it. Of particular note if you've already done the Yorgey course are the modules on phantom types, information flow control, language extensions, concurrency, pipes, and lenses.
You should do Yorgey's course before attempting this: https://github.com/NICTA/course/
Learn You a Haskell for Great Good (LYAH) and Real World Haskell (Thanks bos!) are available online.
I recommend RWH as a reference (thick book). The chapters for parsing and monads are great for getting a sense for where monads are useful. Other people have said that they've liked it a lot. Perhaps a good follow-up for practical idioms after you've got the essentials of Haskell down?
Useful for understanding Functor
, Applicative
, Monad
, Monoid
and other typeclasses in general but also some Hask-specific category theory:
In addition to being an amazing guide for all kinds of things such as GADTs, this also covers some useful basics for Cabal
Cabal Hell was a problem for Haskell users before the introduction of sandboxes. Installing outside of a sandbox will install into your user package-db. This is not a good idea except for foundational packages like Cabal, alex, and happy. Nothing else should be installed in the user or global package-dbs unless you know what you're doing.
To experiment with a package or start a project, begin by doing cabal sandbox init
in a new directory.
Put briefly:
- Always use sandboxes for installing new packages, building new or existing projects, or starting experiments
- Use
cabal repl
to start a project-scoped ghci instance
The sandbox-based approach I suggest should avoid package-dependency problems, but it's incompatible with the way the Haskell Platform provides pre-built packages. If you're still learning Haskell and don't understand how ghc-pkg and Cabal work, avoid Platform and instead use the install instructions earlier in the guide.
The Hoogle search engine can search by type:
http://www.haskell.org/hoogle/?hoogle=%28a+-%3E+b%29+-%3E+%5ba%5d+-%3E+%5bb%5d
Alternately:
https://www.fpcomplete.com/hoogle
Also Hayoo (which has all of hackage enabled for search by default): http://holumbus.fh-wedel.de/hayoo/hayoo.html
https://gist.github.com/bitemyapp/3e6a015760775e0679bf
- First: http://fuuzetsu.co.uk/blog/posts/2014-01-06-Fix-your-Hackage-documentation.html
- Second: http://fuuzetsu.co.uk/blog/posts/2014-01-06-Hackage-documentation-v2.html
Note that these posts are slightly out of date: for example, now Hackage sports shiny new info with documentation info and build status.
In order to have haddocks include documentation for related packages, you have to set documentation: True in your ~/.cabal/config. If it was left on the default (False) or set to False, you'll have to delete all your packages and reinstall before generating haddocks.
The other thing to keep in mind is that due to the way the $pkg parameter gets interpolated by cabal, not by you, the html-location and content-location parameters must be in single quotes and entered into a shell or contained in a shell script. They will not work in a Makefile, because it will think they are Make variables!
#!/usr/bin/env sh
cabal haddock --hoogle --hyperlink-source --html-location='http://hackage.haskell.org/package/$pkg/docs' --contents-location='http://hackage.haskell.org/package/$pkg'
- Excellent article: http://www.haskellforall.com/2014/10/how-to-desugar-haskell-code.html
If you're as big a fan of TravisCI as I am, then I strongly recommend you look at using https://github.com/hvr/multi-ghc-travis as the basis of the travis.yml for your Haskell projects.
After you're comfortable with Haskell, strongly consider learning Lenses and Prisms, even if just as a "user". You don't need to understand the underlying category for it to be useful.
Seen here: http://hackage.haskell.org/package/lens
We have an embarrassment of riches! There are three main choices I would recommend:
-
Haste
-
http://www.airpair.com/haskell/posts/haskell-tutorial-introduction-to-web-apps Excellent demo of Haste for an example project
-
GHCJS
-
PureScript (not strictly Haskell like Haste and GHCJS, but a popular choice among Haskellers)
-
http://www.christopherbiscardi.com/2014/06/22/getting-started-with-purescript/ Great guide for getting started
GHCJS and Haste are both fully Haskell. GHCJS will work with more Haskell packages than Haste, but this doesn't affect a lot of frontend projects. PureScript isn't Haskell at all, so direct code sharing with your backend will not work.
GHCJS has the fattest runtime payload overhead at about 100kb (luite is working on this). Haste and PureScript are competitive.
PureScript has the best JS tooling integration (uses gulp/grunt/bower), GHCJS and Haste integrate better with Haskell's tooling (Cabal).
All three are great choices and will work for most frontend projects.
-
Parser combinator tutorial for Haskell using Parsec: https://github.com/JakeWheat/intro_to_parsing
-
Writing your own micro-Parsec: http://olenhad.me/articles/monadic-parsers/
-
http://blog.raynes.me/blog/2012/11/27/easy-json-parsing-in-haskell-with-aeson/
-
http://bitemyapp.com/posts/2014-04-11-aeson-and-user-created-types.html
-
http://bitemyapp.com/posts/2014-04-17-parsing-nondeterministic-data-with-aeson-and-sum-types.html
-
http://chimera.labs.oreilly.com/books/1230000000929/ch02.html Marlow's book about parallelism and concurrency has one of the best introductions to laziness and normal form I've found. Use other material too if it doesn't stick immediately.
-
http://augustss.blogspot.hu/2011/05/more-points-for-lazy-evaluation-in.html
-
http://stackoverflow.com/questions/13042353/does-haskell-have-tail-recursive-optimization
let a = 1 : a -- guarded recursion, (:) is lazy and can be pattern matched.
let (v : _) = a
> v
1
> head a -- head a == v
1
let a = 1 * a -- not guarded, (*) is strict
> a
*** Exception: <<loop>>
-
http://chimera.labs.oreilly.com/books/1230000000929 This book by Simon Marlow is probably the best I've ever read on the topics of Parallelism and Concurrency:
-
http://kukuruku.co/hub/haskell/haskell-testing-a-multithread-application A thorough walk-through on testing & incremental development of a multi-threaded application in Haskell
-
http://www.haskell.org/haskellwiki/Functional_Reactive_Programming
People vastly overestimate the difficulty of using Lens. Anybody comfortable with Functor/Foldable/Traversable (or even just the first one) can leverage lenses and prisms to make their life happier.
If you've ever done something like: (fmap . fmap)
you were "lensing" in your head.
I recommend these two tutorials/introductions:
Implement the standard library monads ( List, Maybe, Cont, Error, Reader, Writer, State ) for yourself to understand them better. Then maybe write an monadic interpreter for a small expression language using Monad Transformers Step by Step paper.
Writing many interpreters by just changing the monad to change the semantics can help convey what's going on.
- https://vimeo.com/73648150 This talk by Tony excellently motivates monad transformers
Also, reimplement Control.Monad. Functions like mapM
or sequence
are good opportunities to practice writing generic monadic code.
The NICTA course can be used as a guide to this process, which will also involve writing your own Applicative as well.
From:
- http://www.reddit.com/r/haskell/comments/29eke6/basic_program_ideas_for_learning_about_monads/cik5aj6
- http://www.reddit.com/r/haskell/comments/29eke6/basic_program_ideas_for_learning_about_monads/cik5trg
-
http://www.cs.virginia.edu/~wh5a/personal/Transformers.pdf (warning, code out of date)
- https://github.com/kazu-yamamoto/unit-test-example/blob/master/markdown/en/tutorial.md This tutorial by Kazu Yamamoto is fantastic.
- Overview of resourceT by Snoyman: https://www.fpcomplete.com/user/snoyberg/library-documentation/resourcet
- https://github.com/jwiegley/simple-conduit Good simple library for learning how streaming IO works in general, knowledge transferrable to libraries like Pipes and Conduit
Some of the crazy *-morphism words you've heard are actually about recursion. NB - before tackling this material you should know how to implement foldr for lists and at least one other data structure, such as a tree. (folds are catamorphisms) Knowing how to implement an unfold (anamorphism) for the same will round things out a bit.
This material dovetails with traversable and foldable.
-
http://patrickthomson.ghost.io/an-introduction-to-recursion-schemes/
-
http://fho.f12n.de/posts/2014-05-07-dont-fear-the-cat.html - good demonstration of how hylomorphism is the composition of cata and ana.
-
http://comonad.com/reader/2009/recursion-schemes/ - this field guide is excellent.
-
http://eprints.eemcs.utwente.nl/7281/01/db-utwente-40501F46.pdf
-
https://www.fpcomplete.com/user/edwardk/recursion-schemes/catamorphisms
If you want to follow up on the type and category theory:
-
http://byorgey.wordpress.com/2014/01/14/catsters-guide/ and http://byorgey.wordpress.com/catsters-guide-2/
-
http://www.cs.cmu.edu/~rwh/plbook/book.pdf Harper's Practical Foundations for Programming Languages is the best PL focused intro to type theory I've read.
-
http://www.quora.com/Category-Theory/What-is-the-best-textbook-for-Category-theory?share=1 Kmett's recommendations
-
http://en.wikibooks.org/wiki/Haskell/Category_theory nice diagrams
-
http://www.haskell.org/haskellwiki/Category_theory good links to other resources
-
http://science.raphael.poss.name/categories-from-scratch.html includes practical examples
-
https://www.google.com/search?q=Awodey+Category+Theory the standard text along with MacLane
-
http://www.cis.upenn.edu/~bcpierce/courses/670Fall04/GreatWorksInPL.shtml Pierce's Great Works in PL list
Didn't know where else to put these:
-
http://creativelad.wordpress.com/2013/11/28/final-encodings-part-1-a-quick-demonstration/
-
http://martijn.van.steenbergen.nl/journal/2009/10/18/transforming-polymorphic-values/
-
http://martijn.van.steenbergen.nl/journal/2009/11/12/gadts-in-haskell-98/
-
https://www.fpcomplete.com/user/mutjida/typed-tagless-final-linear-lambda-calculus
-
http://existentialtype.wordpress.com/2011/03/21/the-dog-that-didnt-bark/ less specifically relevant but interesting
Comment from Reddit thread by glaebhoerl
Interesting side note: GHC needs to hide the state token representation behind an abstract IO type because the state token must always be used linearly (not duplicated or dropped), but the type system can't enforce this. Clean, another lazy Haskell-like language, has uniqueness types (which are like linear types and possibly different in ways I'm not aware of), and they expose the World-passing directly and provide a (non-abstract) IO monad only for convenience.
-
http://blog.ezyang.com/2011/05/unraveling-the-mystery-of-the-io-monad/
-
http://hackage.haskell.org/package/base-4.7.0.1/docs/System-IO-Unsafe.html#v:unsafePerformIO Read the docs and note implementation of unsafeDupablePerformIO
-
https://ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/CoreSynType
-
http://book.realworldhaskell.org/read/profiling-and-optimization.html
-
http://bitemyapp.com/posts/2014-04-05-grokking-sums-and-constructors.html squint hard.
-
http://okmij.org/ftp/Computation/lightweight-dependent-typing.html
-
https://hackage.haskell.org/package/fgl particularly the shortest path algos here which are pure: http://hackage.haskell.org/package/fgl-5.4.2.2/docs/Data-Graph-Inductive-Query-SP.html
-
http://web.engr.oregonstate.edu/~erwig/papers/abstracts.html#JFP01
-
http://web.engr.oregonstate.edu/~erwig/fgl/haskell/old/fgl0103.pdf
-
http://hackage.haskell.org/package/containers-0.5.5.1/docs/Data-Graph.html
For any users (usually Yesod users) that have build problems, consider Stackage.
- A good summary of Stackage is here: https://www.fpcomplete.com/blog/2014/05/stackage-server
In the author's opinion, Stackage is usually more useful than cabal freeze
.
Dialogues these are actually pretty important and helpful. Look here for deep dives on a variety of topics.