
I worked my way throughLearn You a Haskell for Great Good! about six months ago and I haven't worked much with Haskell since then. So of course I forgot everything. So I've worked back through it again and made this handy refresher guide (mostly for myself but also) for you to have a quick reminder how things work in Haskell-land:
not
Haskell doesn't use~
or!
to denote negation, rather, it uses thenot
function:
ghci>notTrue-- comments come after two hyphensFalse
...but it does use/=
to mean "is not equal to":
ghci>5/=4True
function application
Haskell doesn't use parentheses to separate a function's name from its arguments; rather, we just use spaces:
ghci>max344
partial function application
Partially-applied functions can be saved in variables and used later!
ghci>min5=min5ghci>min544ghci>min565
infix notation
Some functions make more sense when they appear between their two parameters, rather than before them. For instance:
ghci>div1005-- prefix notation20ghci>100`div`5-- infix notation20
functions
Define basic functions by putting the named parameters before the=
and the function definition after:
ghci>pythagorasab=sqrt(a*a+b*b)ghci>pythagoras51213.0
Note that function names can contain'
(usually used to denote strict versions of otherwise lazy functions) and cannot begin with capital letters.
lists
In Haskell, lists are homogeneous and strings are lists of characters:
ghci>ll=[1,2,3,4]ghci>ss=['e','l','l','o']
A single element can be prepended (fast) to a list with:
and a list can be appended (slow) to a list with++
:
ghci>'h':ss"hello"ghci>ll++[5]++[6,7][1,2,3,4,5,6,7]ghci>'s':'m':ss-- : and ++ can be chained like this"smello"
Elements can be extracted with the!!
operator (unlike the[]
operator in C):
ghci>ll!!2-- equivalent to ll[2] in C3
A list can itself contain lists of items or lists of lists of items and all of those lists can be different lengths, as long as they all contain the same type of objects (numbers or characters, but not both).
Finally, lists are compared in lexicographical order, meaning that the first elements are compared first, then the second elements, etc:
ghci>[1,2,3]>[0,4,5]Trueghci>[1,2,3]>[1,2]True
list functions
head
/tail
/init
/last
/length
ghci>ll[1,2,3,4]ghci>headll-- first element of list1ghci>tailll-- all but first element of list[2,3,4]ghci>initll-- all but last element of list[1,2,3]ghci>lastll-- last element of list4ghci>lengthll-- length of list4
Note thathead
,tail
,init
andlast
all throw errors if they're called on an empty list, butlength
returns 0. You can check if a list is empty withnull
:
ghci>length[]-- length of an empty list is zero0ghci>lengthll-- ll has 4 elements in it at indices 0..34ghci>null[]-- an empty list is nullTrueghci>nullllFalse
reverse
ghci>reversell-- reverse a list[4,3,2,1]
take
/drop
ghci>take1ll-- take 1 element from the beginning of the list[1]ghci>take3ll-- take 3 elements[1,2,3]ghci>drop1ll-- drop 1 element from the beginning of the list[2,3,4]ghci>drop3ll-- drop 3 elements[4]
maximum
/minimum
ghci>maximumll4ghci>minimumll1
sum
/product
ghci>sumll-- 1 + 2 + 3 + 410ghci>productll-- 1 * 2 * 3 * 424
elem
elem
works like sort of like howcontains()
works in other languages, it returns true if the first argument is an element of the second argument, which must be a list:
ghci>elem2llTrueghci>5`elem`llFalse
cycle
/repeat
/replicate
Repeat a list withcycle
; repeat a single object withrepeat
ghci>take10(cyclell)[1,2,3,4,1,2,3,4,1,2]ghci>take10(repeat5)[5,5,5,5,5,5,5,5,5,5]ghci>take5(repeatll)[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]ghci>replicate105-- same as take 10 (repeat 5)[5,5,5,5,5,5,5,5,5,5]
ranges
Ranges work with integers and characters:
ghci>[1..10][1,2,3,4,5,6,7,8,9,10]ghci>['a'..'j']"abcdefghij"
The first two elements of a list can be given to define a pattern:
ghci>['z','w'..'a']"zwtqnkheb"ghci>[26,23..1][26,23,20,17,14,11,8,5,2]
Above, the list ends when the last element is hit, but you can also just define how many elements you want by usingtake
:
ghci>take10[1,3..][1,3,5,7,9,11,13,15,17,19]
Coming up in Part 2: list comprehensions, tuples, and typeclasses
Top comments(19)

- Email
- LocationNY
- EducationMount Allison University
- PronounsHe/him
- WorkCo-founder at Forem
- Joined
Great post, I really want to get into Haskell

Have you triedTry Haskell?

- Email
- LocationOttawa, Canada
- EducationPh.D. in [Astroparticle] Physics
- Pronounshe / him
- WorkPrincipal Consultant at Improving
- Joined
I missed this comment and just checked this out today... it's great! I'll have to keep this link for reference.

- Email
- LocationNY
- EducationMount Allison University
- PronounsHe/him
- WorkCo-founder at Forem
- Joined
Nope. This is great!

- Joined
Just a minor nitpick,not
is not a keyword, but a function

- Email
- LocationOttawa, Canada
- EducationPh.D. in [Astroparticle] Physics
- Pronounshe / him
- WorkPrincipal Consultant at Improving
- Joined
Fixed!

- Email
- LocationOttawa, Canada
- EducationPh.D. in [Astroparticle] Physics
- Pronounshe / him
- WorkPrincipal Consultant at Improving
- Joined
Right! That's another idiosyncrasy of Haskell that I neglected to mention.

I'm fine with it. SQL's not-equal-to operator is<>
things like that really don't matter. It's like being upset about single quotes vs double quotes. If you think that's annoying, then you should absolutely learn Haskell, because you're focused on aesthetics instead of big picture implications, and Haskell will force you to learn better ways of thinking about the big picture.
Most of the problems that most of us have are simply difficult and frowned upon in Haskell.

- Email
- LocationOttawa, Canada
- EducationPh.D. in [Astroparticle] Physics
- Pronounshe / him
- WorkPrincipal Consultant at Improving
- Joined
I think maybe you misunderstood what one of us said. All we did was note that the "not equals" operator is different than the "normal" one in Haskell. Which is a good thing to know whether you're focused on aesthetics or not.

LYAH is a nice introduction to Haskell but if you're serious about understanding the language I'd recommend givingHaskell Programming from First Principles a read.
Just curious. Is there any particular application that you'd like to build in Haskell?

- Email
- LocationBoston, MA, USA
- EducationCurrently enrolled @ Champlain College Online
- WorkRust Developer @ Tangram.dev
- Joined
Nice and concise! I'd completely forgotten aboutcycle

- Email
- LocationDenver, CO
- EducationHamilton College
- WorkDeveloper things at AWS
- Joined
Thank you so much for this,@joshcheek writes Haskell for some coding puzzles and I really want to learn it as a result!

Free Great Book:Learn You a Haskell

Good post, and another intro video.

- LocationScotland
- EducationSomething something cybernetics
- PronounsThey/them
- WorkGeneral-purpose software person
- Joined
I worked my way throughLearn you about six months ago and have also forgotten most of it!
For further actions, you may consider blocking this person and/orreporting abuse