summaryrefslogtreecommitdiffhomepage
path: root/haskell.html.markdown
blob: 5ffc63f35524ab0118c44fbf114ddbd4fa8dd84d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
---
language: haskell
author: Adit Bhargava
author_url: http://adit.io
---

Haskell was designed as a practical, purely functional programming language. It's famous for
it's monads and it's type system, but I keep coming back to it because of it's elegance. Haskell
makes coding a real joy for me.

```haskell
-- Single line comments start with two dashes.
{- Multiline comments can be enclosed
in a block like this.
-}

----------------------------------------------------
-- 1. Primitive Datatypes and Operators
----------------------------------------------------

-- You have numbers
3 -- 3

-- Math is what you would expect
1 + 1 -- 2
8 - 1 -- 7
10 * 2 -- 20
35 / 5 -- 7.0

-- Division is not integer division by default
35 / 4 -- 8.75

-- integer division
35 `div` 4 -- 8

-- Boolean values are primitives
True
False

-- Boolean operations
not True -- False
not False -- True
1 == 1 -- True
1 /= 1 -- False
1 < 10 -- True

-- Strings and characters
"This is a string."
'a' -- character
'You cant use single quotes for strings.' -- error!

-- Strings can be added too!
"Hello " ++ "world!" #=> "Hello world!"

-- A string can be treated like a list of characters
"This is a string" !! 0 #=> 'T'


----------------------------------------------------
-- Lists and Tuples
----------------------------------------------------

-- Every element in a list must have the same type.
-- Two lists that are the same
[1, 2, 3, 4, 5]
[1..5]

-- You can also have infinite lists in Haskell!
[1..] -- a list of all the natural numbers

-- joining two lists
[1..5] ++ [6..10]

-- adding to the head of a list
0:[1..5] -- [0, 1, 2, 3, 4, 5]

-- indexing into a list

[0..] !! 5 -- 4

-- more list operations

head [1..5] -- 1
tail [1..5] -- [2, 3, 4, 5]
init [1..5] -- [1, 2, 3, 4]
last [1..5] -- 5

-- list comprehensions
[x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10]

-- with a conditional
[x*2 | x <- [1..5], x*2 > 4] # [6, 8, 10]

-- Every element in a tuple can be a different type, but a tuple has a fixed length.
-- A tuple:
("haskell", 1)

-- accessing elements of a tuple
fst ("haskell", 1) -- "haskell"
snd ("haskell", 1) -- 1

----------------------------------------------------
-- 3. Functions
----------------------------------------------------
-- A simple function that takes two variables
add a b = a + b

-- Using the function
add 1 2 -- 3

-- You can also put the function name between the two arguments with backticks:
1 `add` 2 -- 3

-- You can also define functions that have no characters! This lets you define
-- your own operators:

-- Here's an operator that does integer division
(//) a b = a `div` b
35 // 4 -- 8

-- Guards: an easy way to do branching in functions
fib x
  | x < 2 = x
  | otherwise = fib (x - 1) + fib (x - 2)

-- You can do the same thing with pattern matching.
fib 1 = 1
fib 2 = 2
fib x = fib (x - 1) + fib (x - 2)

-- So we have given three different definitions for fib.
-- Haskell will automatically call the first function that matches
-- the pattern of the value. 

-- Pattern matching on tuples:
foo (x, y) = (x + 1, y + 2)

-- Pattern matching on arrays. Here `x` is the first element
-- in the array, and `xs` is the rest of the array:
map func [x] = [func x]
map func (x:xs) = func x:(map func xs)

-- Anonymous functions are created with a backslash followed by all the arguments.
map (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7]

-- using fold (called `inject` in some languages) with an anonymous function.
-- foldl1 means fold left, and use the first value in the array as the initial
-- value for the accumulator.
foldl1 (\acc x -> acc + x) [1..5] -- 15

----------------------------------------------------
-- 4. Some fancy things you can do with functions
----------------------------------------------------

-- currying: if you don't pass in all the arguments to a function,
it gets "curried". That means it returns a function that takes the 
rest of the arguments.

add a b = a + b
foo = add 10 -- foo is now a function that takes a number and adds 10 to it
foo 5 -- 15

-- Another way to write the same thing
foo = (+10)
foo 5 -- 15

-- function composition
the (.) function chains functions together.
For example, here foo is a function that takes a value. It adds 10 to it,
multiplies the result of that by 5, and then returns the final value.

foo = (*5) . (+10)

-- (5 + 10) * 5 = 75
foo 5 -- 75

-- fixing precedence
-- Haskell has another function called `$`. This changes the precedence
so that everything to the left of it gets computed first and then applied
to everything on the right. You can use `.` and `$` to get rid of a lot
of parentheses:

-- before
(even (double 7)) -- true

-- after
even . double $ 7 -- true

----------------------------------------------------
-- 5. Type signatures
----------------------------------------------------

Haskell has a very strong type system, and everything has a type signature.

Some basic types:
5 :: Integer
"hello" :: String
True :: Bool

Functions have types too.
Not takes a boolean and returns a boolean:
not :: Bool -> Bool

Here's a function that takes two arguments:
add :: Integer -> Integer -> Integer

----------------------------------------------------
-- 6. Control Flow
----------------------------------------------------

-- if statements
haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome"

-- if statements can be on multiple lines too, indentation is important
haskell = if 1 == 1
            then "awesome"
            else "awful"

-- case statements

-- Here's how you could parse command line arguments in Haskell

case args of
  "help" -> printHelp
  "start" -> startProgram
  _ -> putStrLn "bad args"


-- loops: recursion
-- Haskell doesn't have loops because it uses recursion instead.

-- map a function over every element in an array

map (*2) [1..5] -- [2, 4, 6, 8, 10]

-- you can make a for function using map
for array func = map func array

-- and then use it

for [0..5] $ \i -> print i

----------------------------------------------------
-- 7. Data Types
----------------------------------------------------

-- Here's how you make your own data type in Haskell

data Color = Red | Blue | Green

Now you can use it in a function:

say :: Color -> IO String
say Red = putStrLn "You are Red!"
say Blue = putStrLn "You are Blue!"
say Green = putStrLn "You are Green!"

-- Your data types can have parameters too:

data Maybe a = Nothing | Just a

-- These are all of type Maybe
Nothing
Just "hello"
Just 1

----------------------------------------------------
-- 8. The Haskell REPL
----------------------------------------------------

-- Start the repl by typing `ghci`.
-- Now you can type in Haskell code. Any new values
-- need to be created with `let`:

let foo = 5

-- You can see the type of any value with `:t`:

>:t foo
foo :: Integer
```

There's a lot more to Haskell, including typeclasses and monads. These are the big ideas that make Haskell such fun to code in. I'll leave you with one final Haskell example: an implementation of quicksort in Haskell:

```haskell
qsort [] = []
qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater
    where lesser  = filter (< p) xs
          greater = filter (>= p) xs
```