Skip to content

Commit 153bd39

Browse files
committed
Trying a different, more fine structure.
1 parent db31ca2 commit 153bd39

File tree

7 files changed

+521
-132
lines changed

7 files changed

+521
-132
lines changed

.vscode/tasks.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "0.1.0",
5+
"command": "make",
6+
"isShellCommand": true,
7+
"options": {
8+
"cwd": "${workspaceRoot}/script/"
9+
},
10+
"tasks": [
11+
{
12+
"taskName": "html",
13+
"args": [
14+
"html"
15+
],
16+
"isBuildCommand": true,
17+
"showOutput": "silent",
18+
"problemMatcher": {
19+
"owner": "reStructuredText",
20+
"fileLocation": ["relative", "${workspaceRoot}"],
21+
"pattern": [
22+
{
23+
"regexp": "^(.*):(\\d+):(\\d+):\\s+(WARNING|ERROR|INFO):\\s+(.*)$",
24+
"file": 1,
25+
"line": 2,
26+
"column": 3,
27+
"severity": 4,
28+
"message": 5
29+
}
30+
]
31+
}
32+
},
33+
{
34+
"taskName": "pdf",
35+
"args": [
36+
"latexpdf"
37+
],
38+
"showOutput": "silent"
39+
}
40+
]
41+
}

script/source/compiler.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Haskell sources and the compiler
2-
================================
1+
Sources and the compiler
2+
========================
33

44
Haskell is a compiled language.
55
As such you do not require any special tooling at runtime.

script/source/exercises/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Haskell exercises
2-
=================
1+
Exercises
2+
=========
33

44

55
.. toctree::

script/source/functions.rst

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
.. _functions:
2+
3+
Functions
4+
=========
5+
6+
7+
.. _lambdas:
8+
9+
Function literals
10+
-----------------
11+
12+
Function literals in Haskell are also often called **lambda functions**.
13+
The syntax is a slash ``\`` followed by a list of space separated paramters, follwed by an ASCII arrow ``->`` upon which follows the body of the function.
14+
Function bodies in Haskell are always an expression, and as such require no ``return`` keyword.
15+
16+
::
17+
18+
\ param -> param
19+
20+
Here for instance we have a function which takes one parameter as input and return it.
21+
This function is also known as ``id``.
22+
23+
::
24+
25+
-- we often call an unspecified parameter 'x'
26+
let id = \x -> x
27+
28+
Haskell is a functional language.
29+
As such functions may be used just like any other value including being assigned to bindings.
30+
The type of our binding is now the function type.
31+
32+
::
33+
34+
let id :: a -> a
35+
id = \x -> x
36+
37+
When we have a value of the function type we may apply it to an argument of the type *left* of the arrow to obtain a value of the type *right* of the arrow.
38+
Ergo ``Int -> Bool`` applied to ``Int`` gives a ``Bool``.
39+
Similarly ``a -> a`` applied to ``Int`` gives an ``Int`` again.
40+
An ``a -> a`` applied to a ``Bool`` gives a ``Bool``.
41+
42+
To apply a function we ise the simplest synta of all, juxtaposition.
43+
Also called *postfix notation* or "the function followed by the arguments, all space separated".
44+
45+
::
46+
47+
let id :: a -> a
48+
id = \x -> x
49+
50+
myBool = id True
51+
myBool2 = (\x -> x) True
52+
myInt = id 5
53+
54+
myBool == myBool2 == True && myInt == 5
55+
56+
Lets look at another example fuction:
57+
58+
::
59+
60+
let const :: a -> b -> a
61+
const = \x _ -> x
62+
63+
The ``const`` function takes a first parameter ``x`` and a second parameter, which we ignore.
64+
The underscore ``_`` as a parameter or binding name is used to indicate that we ignore the value.
65+
And finally the function returns the first parameter.
66+
67+
Note that the type of the function is now ``a -> b -> a``.
68+
We see here that the function type ``->`` occurs twice and this is deliberate because we may rewrite our function as follows:
69+
70+
::
71+
72+
let const :: a -> (b -> a)
73+
const = \x -> \_ -> x
74+
75+
Now we can see the analogy.
76+
We first consume the first parameter and return a function.
77+
This second function is then applied to the second parameter returning the final value.
78+
The two versions ``\x _ -> x`` and ``\x -> \_ -> x`` and their type signatures are equivalent in Haskell, hence the same type.
79+
80+
The practical upshot of this is that haskell makes it extremely easy to do what is often called "partially applied functions".
81+
This means supplying fewer arguments to a function than would be required to produce its final value.
82+
Technically this is not even possible in Haskell, since, as we have seen above, every Haskell function only takes one argument but may return a curried function to simulate being given a second argument.
83+
To fully grasp the possibilities that partial application offers it is instrumental to internalise this aforementioned concept.
84+
85+
Partial application is best described using examples:
86+
87+
::
88+
89+
let const :: a -> b -> a
90+
const x _ = x
91+
92+
alwaysFive = const 5
93+
94+
alwaysFive "a string" == alwaysFive 6 == alwaysFive () == 5
95+
96+
let plusThree = (+ 3)
97+
98+
plusThree 5 == 8
99+
plusThree 10 == 13
100+
101+
.. admonition:: Aside
102+
103+
This is particularly useful when combined with :ref:`higher order functions`.
104+
105+
For instance we can increment a whole list of integers using the partial application of ``+`` to ``1``.
106+
107+
::
108+
109+
map (+ 1) [4,5,8] == [5,6,9]
110+
111+
Or to find the index of a particular element in a list: (partial application of ``==``)
112+
113+
::
114+
115+
find (== 6) [3,6,8] == Just 2
116+
117+
Note that these are advanced examples, there is no need to undestand them yet, we will cover those in detail later.
118+
119+
120+
Syntactic sugar for function definitions
121+
----------------------------------------
122+
123+
There are a few common patterns in Haskell when defining functions.
124+
The first is for creating function values.
125+
126+
::
127+
128+
myFunction = \a b -> doSomthing
129+
130+
let anotherFunction = \x -> expr
131+
132+
This pattern is very common.
133+
Therefore we have some syntactic sugar in the Haskell laguage which allows us to omit both the backslash ``\`` and the arrow ``->`` by moving the function arguments before the equal sign.
134+
135+
::
136+
137+
myFunction a b = doSomthing
138+
139+
let anotherFunction x = expr
140+
141+
Another common pattern is to take arguments to a function and immediately perform a ``case`` match on them.
142+
143+
::
144+
145+
myFunction a =
146+
case a of
147+
Constr1 val -> ...
148+
Constr2 v2 -> ...
149+
150+
Instead we may write
151+
152+
::
153+
154+
myFunction (Constr1 val) = ...
155+
myFunction (Constr2 v2) = ...
156+
157+
.. note::
158+
Here we must use parentheses around the match clauses to distinguish the clauses for several arguments.
159+

script/source/index.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
You can adapt this file completely to your liking, but it should at least
44
contain the root `toctree` directive.
55
6-
Welcome to Haskell Lessons's documentation!
7-
===========================================
6+
The Haskell Lessons's script
7+
============================
88

99
.. toctree::
1010
:maxdepth: 2
11-
:caption: Contents:
11+
:caption: Capters:
12+
:numbered:
1213

1314
compiler
1415
syntax
16+
functions
17+
types
1518
build-tools
1619
exercises/index
1720

0 commit comments

Comments
 (0)