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.
The haskell mailing lists.
See Chris Done's post on teaching
Be kind and courteous. Being mean or rude scares people off and makes people not want to participate.
Low-effort criticism is for the benefit of the person delivering it, not the person receiving it.
Don't describe things as "easy" or "trivial". You'll making people feel terrible for having to work hard for their progress. Slow learners are often the most thorough learners, this is something to celebrate!
No feigning surprise. Don't act surprised when somebody says they don't know something. They'll feel terrible and you've accomplished nothing except to make yourself feel sharp.
No well-actually's. When someone says something that's almost - but not entirely - correct, and you say, "well, actually…" and then give a minor correction. This is especially annoying when the correction has no bearing on the actual conversation. This doesn't mean Recurse Center isn't about truth-seeking or that we don't care about being precise. Almost all well-actually's are about grandstanding, not truth-seeking.
No back-seat driving. If you see people working through a problem, you shouldn't intermittently interject advice. Let them work it out unless someone asks for help. Avoiding interruptions is one of the founding purposes of #haskell-beginners.
No subtle -isms. Racism, sexism, homophobia, transphobia, and other kinds of bias are not welcome and will not be tolerated.
Guidelines by the Recurse Center manual. Thanks for releasing it Recurse Center.
Haskell is a programming language as laid out in the reports, most recent one being in 2010. The report is available as the onlinereport.
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.
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.
Specifically:
$ sudo apt-get update
$ sudo apt-get install python-software-properties # v12.04 and below
$ sudo apt-get install software-properties-common # v12.10 and above
$ sudo add-apt-repository -y ppa:hvr/ghc
$ sudo apt-get update
$ sudo apt-get install cabal-install-1.20 ghc-7.8.4 happy-1.19.4 alex-3.1.3
Then add the following to your $PATH
(bash_profile, zshrc, bashrc, etc):
~/.cabal/bin:/opt/cabal/1.20/bin:/opt/ghc/7.8.4/bin:/opt/happy/1.19.4/bin:/opt/alex/3.1.3/bin
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're not using stable, you can follow the same steps as Ubuntu, but will
have to execute an additional command. Immediately after
sudo add-apt-repository -y ppa:hvr/ghc
is executed 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 this guide written for Mac OS X:
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.8.4 from the unofficial repo (Fedora 22+ will include it in the official one):
$ sudo yum-config-manager --add-repo \
> https://copr.fedoraproject.org/coprs/petersen/ghc-7.8.4/repo/fedora-21/petersen-ghc-7.8.4-fedora-21.repo
$ sudo yum install ghc cabal-install
As stated in petersen/ghc-7.8.4 copr page this ghc cannot be installed in parallel with Fedora/EPEL ghc.
To install Haskell from the official repos on Arch Linux, run
$ sudo 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 # The backslashes
$ \cabal install cabal-install # are intentional
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.
echo $SHELL | xargs basename
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.
- The windows minimal GHC installer
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:
Do this first, this is the primary way I recommend being introduced to Haskell.
Available online.
Brent Yorgey's course is the best I've found so far. 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 Thompson's book and transition to cis194.
This is the course I recommend doing after Yorgey's cis194 course
Available on github here.
This will reinforce and give you experience directly implementing the abstractions introduced in cis194, this is practice which is critical to becoming comfortable with everyday uses of Functor/Applicative/Monad/etc. in Haskell. Doing cis194 and then the NICTA course represents the core recommendation of my guide and is how I teach everyone Haskell.
Provides more material on intermediate topics
Available online.
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.
Excellent article.
Useful for understanding Functor
, Applicative
, Monad
, Monoid
and other
typeclasses in general but also some Hask-specific category theory:
- The Typeclassopedia
-
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.
-
SO question 'Does haskell have laziness?'
-
Johan Tibell's slides from a talk on reasoning about laziness.
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>>
-
Haddocks for System.IO.Unsafe.unsafePerformIO Read the docs and note implementation of unsafeDupablePerformIO
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.
Do not do these until you understand typeclasses, Monoid, Functor, and Applicative!
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 (mentioned in 'monad transformers' below).
Writing many interpreters by just changing the monad to change the semantics can help convey what's going on.
- This talk by Tony excellently motivates monad transformers, the slides.
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.
Credits:
-
This tutorial by Kazu Yamamoto is fantastic.
-
Simple-Conduit: Good simple library for learning how streaming IO works in general, knowledge transferrable to libraries like Pipes and Conduit
-
Parser combinator tutorial for Haskell using Parsec
Aeson is the standard JSON parsing solution in haskell. Available from hackage and github.
-
The fgl package particularly the purely functional shortest path algos.
-
The graphs package.
In addition to being an amazing guide for all kinds of things such as GADTs, this also covers some useful basics for Cabal
- What I wish I knew when learning Haskell also on github here.
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.
Some best practices for avoiding cabal hell are available here.
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.
For any users (usually Yesod users) that have build problems, consider Stackage.
- A good summary of Stackage is here.
In the author's opinion, Stackage is usually more useful than cabal freeze
.
The Hoogle search engine can search by type.
For example, look at the search results for (a -> b) -> [a] -> [b]
here.
Also hosted by fpcomplete here.
Also Hayoo (which has all of hackage enabled for search by default).
Take a look here.
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
# You can write it one one line by skipping the backslashes
cabal haddock --hoogle --hyperlink-source \
--html-location='http://hackage.haskell.org/package/$pkg/docs' \
--contents-location='http://hackage.haskell.org/package/$pkg'
If you're as big a fan of TravisCI as I am, then I
strongly recommend you take a look at
multi-ghc-travis by as the basis of
the travis.yml
for your Haskell projects.
We have an embarrassment of riches! There are three main choices I would recommend:
-
Haste a Haskell to JavaScript compiler
-
- Not strictly Haskell like Haste and GHCJS, but a popular choice among Haskellers
- Written in and inspired by haskell
- Try purescript in you browser here
- 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.
-
Parallel and Concurrent Programming in Haskell. This book by Simon Marlow is probably the best I've ever read on the topics of Parallelism and Concurrency.
-
A thorough walk-through on testing & incremental development of a multi-threaded application in Haskell.
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.
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:
Look here for more information: Lens package on hackage.
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.
-
Don't fear the cat - Good demonstration of how hylomorphism is the composition of cata and ana.
-
Recursion Schemes - This field guide is excellent.
-
Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire
Not needed to actually write Haskell, just for those interested!
If you want to follow up on type and category theory:
-
The haskell wikibook has nice diagrams
-
Category Theory on haskellwiki, also has good links to other resources
-
Categories from scratch, Includes some practical examples.
-
Pierce's Great Works in PL list.
-
Quora Question: What is the best textbook for category theory? Kmett's recommendations
-
Awodey and MacLane. The standard textbooks on category theory.
-
Harper's Practical Foundations for Programming Languages is the best PL focused intro to type theory I've read.
-
TeX sources for the above talk.
-
The dog that didn't bark less specifically relevant but interesting.
-
Free monads for Less, a sequence of three articles by Edward Kmett
Some are already included here
Hosted in this repository here.
These are actually pretty important and helpful. Look here for deep dives on a variety of topics.