




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































Welcome to Learn You a Haskell for Great Good! If you’re reading this, chances are you want to learn Haskell. Well, you’ve come to the right place, but let’s talk about this tutorial a bit first.
I decided to write this because I wanted to solidify my own knowledge of Haskell and because I thought I could help people new to Haskell learn it from my perspective. There are quite a few tutorials on Haskell floating around on the internet. When I was starting out in Haskell, I didn’t learn from just one resource. The way I learned it was by reading several different tutorials and articles because each explained something in a different way than the other did. By going through several resources, I was able put together the pieces and it all just came falling into place. So this is an attempt at adding another useful re- source for learning Haskell so you have a bigger chance of finding one you like.
This tutorial is aimed at people who have experience in imperative pro- gramming languages (C, C++, Java, Python... ) but haven’t programmed in a functional language before (Haskell, ML, OCaml... ). Although I bet that even if you don’t have any significant programming experience, a smart chap like you will be able to follow along and learn Haskell.
The channel #haskell on the freenode network is a great place to ask questions if you’re feeling stuck. People there are extremely nice, patient and understanding to newbies.
I failed to learn Haskell approximately 2 times before finally grasping it because it all just seemed too weird to me and I didn’t get it. But then once it just “clicked” and after getting over that initial hurdle, it was pretty much smooth sailing. I guess what I’m trying to say is: Haskell is great and if you’re interested in programming you should really learn it even if it seems weird at first. Learning Haskell is much like learning to program for the first time — it’s fun! It forces you to think differently, which brings us to the next section
...
system can intelligently figure out a lot about it. If you say a = 5 + 4, you don’t have to tell Haskell that a is a number, it can figure that out by itself. Type inference also allows your code to be more general. If a function you make takes two parameters and adds them together and you don’t explicitly state their type, the function will work on any two parameters that act like numbers.
Haskell is elegant and concise. Because it uses a lot of high level concepts, Haskell programs are usually shorter than their imperative equivalents. And shorter programs are easier to maintain than longer ones and have less bugs.
Haskell was made by some really smart guys (with PhDs). Work on Haskell began in 1987 when a committee of researchers got together to design a kick-ass language. In 2003 the Haskell Report was published, which defines a stable version of the language.
1.3 What you need to dive in
A text editor and a Haskell compiler. You probably already have your favorite text editor installed so we won’t waste time on that. The two main Haskell com- pilers as of right now are GHC (Glasgow Haskell Compiler) and Hugs. For the purposes of this tutorial we’ll be using GHC. I won’t cover much installation details. On Windows it’s just a matter of downloading the installer and clicking “Next” a couple of times and then rebooting your computer. On Debian based Linux distributions you can just do apt-get install ghc6 libghc6-mtl-dev and you’re laughing. I don’t have a Mac but I’ve heard that if you have MacPorts, you can get GHC by doing sudo port install ghc. Also, I think you can do Haskell development with that wacky mouse with one button, although I’m not sure.
GHC can take a Haskell script (they usually have a .hs extension) and compile it but it also has an interactive mode which allows you to interactively interact with scripts. Interactively. You can call functions from scripts that you load and the results are displayed immediately. For learning it’s a lot easier and faster than compiling every time you make a change and then running the program from the prompt. The interactive mode is invoked by typing in ghci at your prompt. If you have defined some functions in a file called, say, myfunctions.hs, you load up those functions by typing in :l myfunctions and then you can play with them, provided myfunctions.hs is in the same folder from which ghci was invoked. If you change the .hs script, just run :l myfunctions again or do :r, which is equivalent because it reloads the current script. The usual workflow for me when playing around in stuff is defining some functions in a .hs file, loading it up and messing around with them and then changing the .hs file, loading it up again and so on. This is also what we’ll be doing here.
Pretty cool, huh? Yeah, I know it’s not but bear with me. A little pitfall to watch out for here is negating numbers. If we want to have a negative number, it’s always best to surround it with parentheses. Doing 5 * -3 will make GHCI yell at you but doing 5 * (-3) will work just fine.
Boolean algebra is also pretty straightforward. As you probably know, && means a boolean and, || means a boolean or. not negates a True or a False. ghci > True && False False ghci > True && True True ghci > False || True True ghci > not False True ghci > not ( True && True ) False Testing for equality is done like so. ghci > 5 == 5 True ghci > 1 == 0 False ghci > 5 /= 5 False ghci > 5 /= 4 True ghci > " hello " == " hello " True What about doing 5 + "llama" or 5 == True? Well, if we try the first snippet, we get a big scary error message! No instance for ( Num [ Char ]) arising from a use of ‘+ ’ at < interactive >:1:0 - Possible fix : add an instance declaration for ( Num [ Char ]) In the expression : 5 + " llama " In the definition of ‘it ’: it = 5 + " llama " Yikes! What GHCI is telling us here is that "llama" is not a number and so it doesn’t know how to add it to 5. Even if it wasn’t "llama" but "four" or "4", Haskell still wouldn’t consider it to be a number. + expects its left and right side to be numbers. If we tried to do True == 5, GHCI would tell us that the types don’t match. Whereas + works only on things that are considered numbers, == works on any two things that can be compared. But the catch is that they both have to be the same type of thing. You can’t compare apples and oranges. We’ll take a closer look at types a bit later. Note: you can do 5 + 4.0 because 5 is sneaky and can act like an integer or a floating-point number. 4.0 can’t act like an integer, so 5 is the one that has to adapt.
You may not have known it but we’ve been using functions now all along. For instance, * is a function that takes two numbers and multiplies them. As you’ve seen, we call it by sandwiching it between them. This is what we call an infix function. Most functions that aren’t used with numbers are prefix functions. Let’s take a look at them.
Functions are usually prefix so from now on we won’t explicitly state that a function is of the prefix form, we’ll just assume it. In most imperative languages functions are called by writing the function name and then writing its parameters in parentheses, usually separated by commas. In Haskell, functions are called by writing the function name, a space and then the parameters, separated by spaces. For a start, we’ll try calling one of the most boring functions in Haskell.
ghci > succ 8 9
The succ function takes anything that has a defined successor and returns that successor. As you can see, we just separate the function name from the parameter with a space. Calling a function with several parameters is also simple. The functions min and max take two things that can be put in an order (like numbers!) and return the one that’s greater.
ghci > min 9 10 9 ghci > min 3.4 3.
ghci > max 100 101 101
Function application (calling a function by putting a space after it and then typing out the parameters) has the highest precedence of them all. What that means for us is that these two statements are equivalent.
ghci > succ 9 + max 5 4 + 1 16 ghci > ( succ 9) + ( max 5 4) + 1 16
However, if we wanted to get the successor of the product of numbers 9 and 10, we couldn’t write succ 9 * 10 because that would get the successor of 9, which would then be multiplied by 10. So 100. We’d have to write succ (9 * 10) to get 91.
If a function takes two parameters, we can also call it as an infix function by surrounding it with backticks. For instance, the div function takes two integers and does integral division between them. Doing div 92 10 results in a 9. But when we call it like that, there may be some confusion as to which number is doing the division and which one is being divided. So we can call it as an infix function by doing 92 ‘div‘ 10 and suddenly it’s much clearer.
Lots of people who come from imperative languages tend to stick to the notion that parentheses should denote function application. For example, in C, you use parentheses to call functions like foo(), bar(1) or baz(3, "haha"). Like we said, spaces are used for function application in Haskell. So those functions in Haskell would be foo, bar 1 and baz 3 "haha". So if you see something like bar (bar 3), it doesn’t mean that bar is called with bar and 3 as parameters. It means that we first call the function bar with 3 as the parameter to get some number and then we call bar again with that number. In C, that would be something like bar(bar(3)).
Now we’re going to make a function that multiplies a number by 2 but only if that number is smaller than or equal to 100 because numbers bigger than 100 are big enough as it is! doubleSmallNumber x = if x > 100 then x else x * Right here we introduced Haskell’s if statement. You’re probably familiar with if statements from other languages. The difference between Haskell’s if statement and if statements in imperative languages is that the else part is mandatory in Haskell. In imperative languages you can just skip a couple of steps if the condition isn’t satisfied but in Haskell every expression and function must return something. We could have also written that if statement in one line but I find this way more readable. Another thing about the if statement in Haskell is that it is an expression. An expression is basically a piece of code that returns a value. 5 is an expression because it returns 5, 4 + 8 is an expression, x + y is an expression because it returns the sum of x and y. Because the else is mandatory, an if statement will always return something and that’s why it’s an expression. If we wanted to add one to every number that’s produced in our previous function, we could have written its body like this. doubleSmallNumber ’ x = ( if x > 100 then x else x *2) + 1
Had we ommitted the parentheses, it would have added one only if x wasn’t greater than 100. Note the ’ at the end of the function name. That apostrophe doesn’t have any special meaning in Haskell’s syntax. It’s a valid character to use in a function name. We usually use ’ to either denote a strict version of a function (one that isn’t lazy) or a slightly modified version of a function or a variable. Because ’ is a valid character in functions, we can make a function like this. conanO ’ Brien = " It ’ s a - me , Conan O ’ Brien! "
There are two noteworthy things here. The first is that in the function name we didn’t capitalize Conan’s name. That’s because functions can’t begin with uppercase letters. We’ll see why a bit later. The second thing is that this function doesn’t take any parameters. When a function doesn’t take any parameters, we usually say it’s a definition (or a name ). Because we can’t change what names (and functions) mean once we’ve defined them, conanO’Brien and the string "It’s a-me, Conan O’Brien!" can be used interchangeably.
2.3 An intro to lists
Much like shopping lists in the real world, lists in Haskell are very useful. It’s the most used data structure and it can be used in a multitude of different ways to model and solve a whole bunch of problems. Lists are SO awesome. In this section we’ll look at the basics of lists, strings (which are lists) and list comprehensions.
In Haskell, lists are a homogenous data structure. It stores several elements of the same type. That means that we can have a list of integers or a list of
characters but we can’t have a list that has a few integers and then a few characters. And now, a list!
Note : We can use the let keyword to define a name right in GHCI. Doing let a = 1 inside GHCI is the equivalent of writing a = 1 in a script and then loading it.
ghci > let lostNumbers = [4 ,8 ,15 ,16 ,23 ,48] ghci > lostNumbers [4 ,8 ,15 ,16 ,23 ,48]
As you can see, lists are denoted by square brackets and the values in the lists are separated by commas. If we tried a list like [1,2,’a’,3,’b’,’c’,4], Haskell would complain that characters (which are, by the way, denoted as a character between single quotes) are not numbers. Speaking of char- acters, strings are just lists of characters. "hello" is just syntactic sugar for [’h’,’e’,’l’,’l’,’o’]. Because strings are lists, we can use list functions on them, which is really handy.
A common task is putting two lists together. This is done by using the ++ operator. ghci > [1 ,2 ,3 ,4] ++ [9 ,10 ,11 ,12] [1 ,2 ,3 ,4 ,9 ,10 ,11 ,12] ghci > " hello " ++ " " ++ " world " " hello world " ghci > [ ’w ’ , ’o ’] ++ [ ’o ’ , ’t ’] " woot "
Watch out when repeatedly using the ++ operator on long strings. When you put together two lists (even if you append a singleton list to a list, for instance: [1,2,3] ++ [4]), internally, Haskell has to walk through the whole list on the left side of ++. That’s not a problem when dealing with lists that aren’t too big. But putting something at the end of a list that’s fifty million entries long is going to take a while. However, putting something at the beginning of a list using the : operator (also called the cons operator) is instantaneous. ghci > ’A ’: " SMALL CAT " " A SMALL CAT " ghci > 5:[1 ,2 ,3 ,4 ,5] [5 ,1 ,2 ,3 ,4 ,5]
Notice how : takes a number and a list of numbers or a character and a list of characters, whereas ++ takes two lists. Even if you’re adding an element to the end of a list with ++, you have to surround it with square brackets so it becomes a list.
[1,2,3] is actually just syntactic sugar for 1:2:3:[]. [] is an empty list. If we prepend 3 to it, it becomes [3]. If we prepend 2 to that, it becomes [2,3], and so on.
Note : [], [[]] and [[],[],[]] are all different things. The first one is an empty list, the seconds one is a list that contains one empty list, the third one is a list that contains three empty lists.
ghci > last [5 ,4 ,3 ,2 ,1] 1
init takes a list and returns everything except its last element.
ghci > init [5 ,4 ,3 ,2 ,1] [5 ,4 ,3 ,2]
If we think of a list as a monster, here’s what’s what.
But what happens if we try to get the head of an empty list?
ghci > head [] *** Exception : Prelude. head : empty list
Oh my! It all blows up in our face! If there’s no monster, it doesn’t have a head. When using head, tail, last and init, be careful not to use them on empty lists. This error cannot be caught at compile time so it’s always good practice to take precautions against accidentally telling Haskell to give you some elements from an empty list.
length takes a list and returns its length, obviously.
ghci > length [5 ,4 ,3 ,2 ,1] 5
null checks if a list is empty. If it is, it returns True, otherwise it returns False. Use this function instead of xs == [] (if you have a list called xs)
ghci > null [1 ,2 ,3] False ghci > null [] True
reverse reverses a list.
ghci > reverse [5 ,4 ,3 ,2 ,1] [1 ,2 ,3 ,4 ,5]
take takes number and a list. It extracts that many elements from the beginning of the list. Watch.
ghci > take 3 [5 ,4 ,3 ,2 ,1] [5 ,4 ,3] ghci > take 1 [3 ,9 ,3] [3] ghci > take 5 [1 ,2] [1 ,2] ghci > take 0 [6 ,6 ,6] []
See how if we try to take more elements than there are in the list, it just returns the list. If we try to take 0 elements, we get an empty list.
drop works in a similar way, only it drops the number of elements from
the beginning of a list.
ghci > drop 3 [8 ,4 ,2 ,1 ,5 ,6] [1 ,5 ,6] ghci > drop 0 [1 ,2 ,3 ,4] [1 ,2 ,3 ,4] ghci > drop 100 [1 ,2 ,3 ,4] []
maximum takes a list of stuff that can be put in some kind of order and returns the biggest element.
minimum returns the smallest. ghci > minimum [8 ,4 ,2 ,1 ,5 ,6] 1 ghci > maximum [1 ,9 ,2 ,3 ,4] 9 sum takes a list of numbers and returns their sum.
product takes a list of numbers and returns their product. ghci > sum [5 ,2 ,1 ,6 ,3 ,2 ,5 ,7] 31 ghci > product [6 ,2 ,1 ,2] 24 ghci > product [1 ,2 ,5 ,6 ,7 ,9 ,2 ,0] 0 elem takes a thing and a list of things and tells us if that thing is an element of the list. It’s usually called as an infix function because it’s easier to read that way. ghci > 4 ‘ elem ‘ [3 ,4 ,5 ,6] True ghci > 10 ‘ elem ‘ [3 ,4 ,5 ,6] False Those were a few basic functions that operate on lists. We’ll take a look at more list functions in section #TODO: include section name and link here.
2.4 Texas ranges
What if we want a list of all numbers between 1 and 20? Sure, we could just type them all out but obviously that’s not a solution for gentlemen who de- mand excellence from their programming languages. Instead, we’ll use ranges. Ranges are a way of making lists that are arithmetic sequences of elements that can be enumerated. Numbers can be enumerated. One, two, three, four, etc. Characters can also be enumerated. The alphabet is an enumeration of characters from A to Z. Names can’t be enumerated. What comes after “John”? I don’t know.
To make a list containing all the natural numbers from 1 to 20, you just write [1..20]. That is the equivalent of writing [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] and there’s no difference between writing one or the other except that writing out long enumeration sequences manually is stupid. ghci > [1..20] [1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12 ,13 ,14 ,15 ,16 ,17 ,18 ,19 ,20] ghci > [ ’a ’.. ’ z ’] " a b c d efgh ijkl mno pqrs tuv wxyz " ghci > [ ’K ’.. ’ Z ’] " KLMNOPQRSTUVWXYZ " Ranges are cool because you can also specify a step. What if we want all even numbers between 1 and 20? Or every third number between 1 and 20?
general sets. A basic comprehension for a set that contains the first ten even natural numbers is S = { 2 · x|x ∈ N, x ≤ 10 }. The part before the pipe is called the output function, x is the variable, N is the input set and x <= 10 is the predicate. That means that the set contains the doubles of all natural numbers that satisfy the predicate.
If we wanted to write that in Haskell, we could do something like take 10 [2,4..]. But what if we didn’t want doubles of the first 10 natural numbers but some kind of more complex function applied on them? We could use a list compre- hension for that. List comprehensions are very similar to set comprehensions. We’ll stick to getting the first 10 even numbers for now. The list comprehension we could use is [x*2 | x <- [1..10]]. x is drawn from [1..10] and for every element in [1..10] (which we have bound to x), we get that element, only doubled. Here’s that comprehension in action. ghci > [ x *2 | x <- [1..10]] [2 ,4 ,6 ,8 ,10 ,12 ,14 ,16 ,18 ,20] As you can see, we get the desired results. Now let’s add a condition (or a predicate) to that comprehension. Predicates go after the binding parts and are separated from them by a comma. Let’s say we want only the elements which, doubled, are greater than or equal to 12.
ghci > [ x *2 | x <- [1..10] , x *2 >= 12] [12 ,14 ,16 ,18 ,20] Cool, it works. How about if we wanted all numbers from 50 to 100 whose remainder when divided with the number 7 is 3? Easy. ghci > [ x | x <- [50..100] , x ‘mod ‘ 7 == 3] [52 ,59 ,66 ,73 ,80 ,87 ,94] Success! Note that weeding out lists by predicates is also called filtering. We took a list of numbers and we filtered them by the predicate. Now for another example. Let’s say we want a comprehension that replaces each odd number greater than 10 with "BANG!" and each odd number that’s less than 10 with "BOOM!". If a number isn’t odd, we throw it out of our list. For convenience, we’ll put that comprehension inside a function so we can easily reuse it.
boomBangs xs = [ if x < 10 then " BOOM! " else " BANG! " | x <- xs , odd x ] The last part of the comprehension is the predicate. The function odd returns True on an odd number and False on an even one. The element is included in the list only if all the predicates evaluate to True. ghci > boomBangs [7..13] [ " BOOM! " ," BOOM! " ," BANG! " ," BANG! " ]
We can include several predicates. If we wanted all numbers from 10 to 20 that are not 13, 15 or 19, we’d do: ghci > [ x | x <- [10..20] , x /= 13 , x /= 15 , x /= 19] [10 ,11 ,12 ,14 ,16 ,17 ,18 ,20] Not only can we have multiple predicates in list comprehensions (an element must satisfy all the predicates to be included in the resulting list), we can also draw from several lists. When drawing from several lists, comprehensions produce all combinations of the given lists and then join them by the output
function we supply. A list produced by a comprehension that draws from two lists of length 4 will have a length of 16, provided we don’t filter them. If we have two lists, [2,5,10] and [8,10,11] and we want to get the products of all the possible combinations between numbers in those lists, here’s what we’d do. ghci > [ x * y | x <- [2 ,5 ,10] , y <- [8 ,10 ,11]] [16 ,20 ,22 ,40 ,50 ,55 ,80 ,100 ,110]
As expected, the length of the new list is 9. What if we wanted all possible products that are more than 50?
ghci > [ x * y | x <- [2 ,5 ,10] , y <- [8 ,10 ,11] , x * y > 50] [55 ,80 ,100 ,110]
How about a list comprehension that combines a list of adjectives and a list of nouns... for epic hilarity. ghci > let nouns = [ " hobo " ," frog " ," pope " ] ghci > let adjectives = [ " lazy " ," grouchy " ," scheming " ] ghci > [ adjective ++ " " ++ noun | adjective <- adjectives , noun <- nouns ] [ " lazy hobo " ," lazy frog " ," lazy pope " ," grouchy hobo " ," grouchy frog " , " grouchy pope " ," scheming hobo " ," scheming frog " ," scheming pope " ]
I know! Let’s write our own version of length! We’ll call it length’. length ’ xs = sum [1 | _ <- xs ]
_ means that we don’t care what we’ll draw from the list anyway so instead of writing a variable name that we’ll never use, we just write _. This function replaces every element of a list with 1 and then sums that up. This means that the resulting sum will be the length of our list.
Just a friendly reminder: because strings are lists, we can use list compre- hensions to process and produce strings. Here’s a function that takes a string and removes everything except uppercase letters from it. removeNonUppercase st = [ c | c <- st , c ‘ elem ‘ [ ’A ’.. ’ Z ’]]
Testing it out: ghci > removeNonUppercase " Hahaha! Ahahaha! " " HA " ghci > removeNonUppercase " IdontLIKEFROGS " " ILIKEFROGS "
The predicate here does all the work. It says that the character will be included in the new list only if it’s an element of the list [’A’..’Z’]. Nested list comprehensions are also possible if you’re operating on lists that contain lists. A list contains several lists of numbers. Let’s remove all odd numbers without flattening the list.
ghci > let xxs = [[1 ,3 ,5 ,2 ,3 ,1 ,2 ,4 ,5] ,[1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9] ,[1 ,2 ,4 ,2 ,1 ,6 ,3 ,1 ,3 ,2 ,3 ,6]] ghci > [ [ x | x <- xs , even x ] | xs <- xxs ] [[2 ,2 ,4] ,[2 ,4 ,6 ,8] ,[2 ,4 ,2 ,6 ,2 ,6]]
You can write list comprehensions across several lines. So if you’re not in GHCI, it’s better to split longer list comprehensions across multiple lines, especially if they’re nested.