DEV Community

Cover image for Relearn You a Haskell (Part 1: The Basics)
Andrew (he/him)
Andrew (he/him)

Posted on

Relearn You a Haskell (Part 1: The Basics)

I worked my way through Learn 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 the not function:

ghci> not True -- comments come after two hyphens
False
Enter fullscreen mode Exit fullscreen mode

...but it does use /= to mean "is not equal to":

ghci> 5 /= 4
True
Enter fullscreen mode Exit fullscreen mode

function application

Haskell doesn't use parentheses to separate a function's name from its arguments; rather, we just use spaces:

ghci> max 3 4
4
Enter fullscreen mode Exit fullscreen mode

partial function application

Partially-applied functions can be saved in variables and used later!

ghci> min5 = min 5

ghci> min5 4
4

ghci> min5 6
5
Enter fullscreen mode Exit fullscreen mode

infix notation

Some functions make more sense when they appear between their two parameters, rather than before them. For instance:

ghci> div 100 5 -- prefix notation
20

ghci> 100 `div` 5 -- infix notation
20
Enter fullscreen mode Exit fullscreen mode

functions

Define basic functions by putting the named parameters before the = and the function definition after:

ghci> pythagoras a b = sqrt (a*a + b*b)

ghci> pythagoras 5 12
13.0
Enter fullscreen mode Exit fullscreen mode

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']
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Elements can be extracted with the !! operator (unlike the [] operator in C):

ghci> ll !! 2 -- equivalent to ll[2] in C
3
Enter fullscreen mode Exit fullscreen mode

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]
True

ghci> [1, 2, 3] > [1, 2]
True
Enter fullscreen mode Exit fullscreen mode

list functions

head / tail / init / last / length

ghci> ll
[1,2,3,4]

ghci> head ll -- first element of list
1

ghci> tail ll -- all but first element of list
[2,3,4]

ghci> init ll -- all but last element of list
[1,2,3]

ghci> last ll -- last element of list
4

ghci> length ll -- length of list
4
Enter fullscreen mode Exit fullscreen mode

Note that head, tail, init and last all throw errors if they're called on an empty list, but length returns 0. You can check if a list is empty with null:

ghci> length [] -- length of an empty list is zero
0

ghci> length ll -- ll has 4 elements in it at indices 0..3
4

ghci> null [] -- an empty list is null
True

ghci> null ll
False
Enter fullscreen mode Exit fullscreen mode

reverse

ghci> reverse ll -- reverse a list
[4,3,2,1]
Enter fullscreen mode Exit fullscreen mode

take / drop

ghci> take 1 ll -- take 1 element from the beginning of the list
[1]

ghci> take 3 ll -- take 3 elements
[1,2,3]

ghci> drop 1 ll -- drop 1 element from the beginning of the list
[2,3,4]

ghci> drop 3 ll -- drop 3 elements
[4]
Enter fullscreen mode Exit fullscreen mode

maximum / minimum

ghci> maximum ll
4

ghci> minimum ll
1
Enter fullscreen mode Exit fullscreen mode

sum / product

ghci> sum ll -- 1 + 2 + 3 + 4
10

ghci> product ll -- 1 * 2 * 3 * 4
24
Enter fullscreen mode Exit fullscreen mode

elem

elem works like sort of like how contains() works in other languages, it returns true if the first argument is an element of the second argument, which must be a list:

ghci> elem 2 ll
True

ghci> 5 `elem` ll
False
Enter fullscreen mode Exit fullscreen mode

cycle / repeat / replicate

Repeat a list with cycle; repeat a single object with repeat

ghci> take 10 (cycle ll)
[1,2,3,4,1,2,3,4,1,2]

ghci> take 10 (repeat 5)
[5,5,5,5,5,5,5,5,5,5]

ghci> take 5 (repeat ll)
[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]

ghci> replicate 10 5 -- same as take 10 (repeat 5)
[5,5,5,5,5,5,5,5,5,5]
Enter fullscreen mode Exit fullscreen mode

ranges

Ranges work with integers and characters:

ghci> [1..10]
[1,2,3,4,5,6,7,8,9,10]

ghci> ['a'..'j']
"abcdefghij"
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

Above, the list ends when the last element is hit, but you can also just define how many elements you want by using take:

ghci> take 10 [1,3..]
[1,3,5,7,9,11,13,15,17,19]
Enter fullscreen mode Exit fullscreen mode

Coming up in Part 2: list comprehensions, tuples, and typeclasses

Top comments (19)

Collapse
 
ben profile image
Ben Halpern

Great post, I really want to get into Haskell

Collapse
 
antonrich profile image
Anton

Have you tried Try Haskell?

Collapse
 
chenge profile image
chenge

This is great for new. I feel Haskell is more easy to remember after you understand the type syntax like :: => ->. This will help you write program more fluently.

A speaker told two break points: monad and category.

Collapse
 
awwsmm profile image
Andrew (he/him)

I missed this comment and just checked this out today... it's great! I'll have to keep this link for reference.

Collapse
 
ben profile image
Ben Halpern

Nope. This is great!

Collapse
 
antonrich profile image
Anton

I really like Haskell's repl because it allows you to learn types quickly.

You just type:

:t the_name_of_the_function.

:t - stands for :type.

Collapse
 
jvanbruegge profile image
Jan van Brügge

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

Collapse
 
awwsmm profile image
Andrew (he/him)

Fixed!

Collapse
 
antonrich profile image
Anton

not True -- comments come after two hyphens

instead of the bang operator (!==) it uses (/=) operator for not True in comparison.

Collapse
 
awwsmm profile image
Andrew (he/him)

Right! That's another idiosyncrasy of Haskell that I neglected to mention.

Collapse
 
joshcheek profile image
Josh Cheek

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.

Thread Thread
 
awwsmm profile image
Andrew (he/him)

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.

Thread Thread
 
drbearhands profile image
DrBearhands

While I get what you're saying, the normal not-equal symbol is . So calling it an idiosyncrasy sounds like c-style bias, as Haskell's version is closer to the mathematical symbol.

Collapse
 
dwayne profile image
Dwayne Crooks

LYAH is a nice introduction to Haskell but if you're serious about understanding the language I'd recommend giving Haskell Programming from First Principles a read.

Just curious. Is there any particular application that you'd like to build in Haskell?

Collapse
 
deciduously profile image
Ben Lovy

Nice and concise! I'd completely forgotten about cycle

Collapse
 
aspittel profile image
Ali Spittel

Thank you so much for this, @joshcheek writes Haskell for some coding puzzles and I really want to learn it as a result!

Collapse
 
chenge profile image
chenge

Free Great Book: Learn You a Haskell

Collapse
 
chenge profile image
chenge • Edited

Good post, and another intro video.

Collapse
 
moopet profile image
Ben Sinclair

I worked my way through Learn you about six months ago and have also forgotten most of it!