Skip to content

Commit 59e6174

Browse files
committed
Added records and if
1 parent 153bd39 commit 59e6174

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

script/source/compiler.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ In this course we will use optional extensions of the Haskell language which not
5555
I therefore highly recommend using the GHC as your Haskell compiler.
5656

5757
For more information about the various parts of the GHC see the `compiler reference pages <https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/>`__.
58-
There you will find information on `compiler flags <https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/flags.html>`__, the `interactive prompt GHCi <ghci reference pages>`_, including the `debugger <https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ghci.html#the-ghci-debugger>`__, `profiling <https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/profiling.html>`__, and the `GHC Haskell extensions <https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#syntactic-extensions>`__.
58+
There you will find information on `compiler flags <https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/flags.html>`__, the `interactive prompt GHCi <ghci-reference-pages>`_, including the `debugger <https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ghci.html#the-ghci-debugger>`__, `profiling <https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/profiling.html>`__, and the `GHC Haskell extensions <https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#syntactic-extensions>`__.
5959
We will discuss all these topics in the future.
6060

6161
We will rarely interact directly with the compiler, as there are very nice :ref:`build tools` out there which we will make use of instead.
6262

63-
.. _ghci reference pages: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ghci.html
63+
.. _ghci-reference-pages: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ghci.html
6464

6565
.. _GHCi:
6666

@@ -70,7 +70,7 @@ The interactive Interpreter (GHCi)
7070
The GHC also supports the interpreted execution of code.
7171
For one this allows you to directly run a Haskell source file with the ``runghc`` or ``runhaskell`` program.
7272
Furthermore any standard installation of GHC includes a program in which you can interactively type Haskell code, inspect it and run it.
73-
The program is called GHCi (``ghci`` is the executable name) which stands for "GHC interpreter". (`ghci reference pages`_)
73+
The program is called GHCi (``ghci`` is the executable name) which stands for "GHC interpreter". (`ghci reference pages <ghci-reference-pages>`_)
7474
GHCi is very simlar to programs like the python or ruby interpreter with the notable difference that the code you type is type checked, like normal Haskell programs, before it is executed.
7575
The GHCi also includes a debugger for Haskell code (similar to `gdb <https://www.sourceware.org/gdb/>`__) which we will :ref:`study in a later chapter <ghci-debugger>`.
7676

script/source/functions.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Ergo ``Int -> Bool`` applied to ``Int`` gives a ``Bool``.
3939
Similarly ``a -> a`` applied to ``Int`` gives an ``Int`` again.
4040
An ``a -> a`` applied to a ``Bool`` gives a ``Bool``.
4141

42-
To apply a function we ise the simplest synta of all, juxtaposition.
42+
To apply a function we use the simplest syntax of all, juxtaposition.
4343
Also called *postfix notation* or "the function followed by the arguments, all space separated".
4444

4545
::
@@ -155,5 +155,5 @@ Instead we may write
155155
myFunction (Constr2 v2) = ...
156156

157157
.. note::
158-
Here we must use parentheses around the match clauses to distinguish the clauses for several arguments.
158+
Here we must use parentheses around the match clauses to distinguish the clauses for the different arguments.
159159

script/source/syntax.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Tuples
9191

9292
More on the tuple type in the next section.
9393

94-
Note that there are not *special* literals for booleans in Haskell as they are just a regular :ref:`data structure <user defined datatypes>`.
94+
Note that there are not *special* literals for booleans in Haskell as they are just a regular :ref:`data structure <user defined types>`.
9595
The literals for ``Bool`` are ``True`` and ``False``.
9696

9797
.. _bindings:
@@ -128,6 +128,24 @@ Note that the second occurrence of ``myInt`` must be properly indented.
128128
We will explore the indentation rules in more detail later.
129129

130130

131+
.. _if:
132+
133+
``if`` expressions
134+
------------------
135+
136+
In Haskell ``if`` is not a statement, but in expression, meaning that it returns a value.
137+
Therefore ``if`` always has a type, and also always has an ``else`` case, which must return a value of the same type.
138+
For instance we can assign the result of ``if`` to a binding.
139+
140+
::
141+
142+
let aBool = False
143+
144+
let anInt = if aBool then 8 else 9
145+
146+
Parentheses are not required and one may write any expression on the branches and for the condition of an ``if``.
147+
148+
131149

132150
.. rubric:: footnotes
133151

script/source/types.rst

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.. _types:
12

23
Types
34
=====
@@ -132,7 +133,7 @@ if we used a ``type`` alias the user could simply pass a ``String`` to the ``sen
132133
The ``case`` construct
133134
----------------------
134135

135-
The ``case`` construct together with function application basically comprise everything which you can do with Haskell at runtime.
136+
The ``case`` construct together with function application basically comprises everything which you can do in Haskell.
136137
The ``case`` construct is used to deconstruct a type and gain access to the data contained withtin.
137138

138139

@@ -296,5 +297,68 @@ Some examples for concrete instances of special types:
296297
Record syntax
297298
-------------
298299

300+
For convenience reasons there is some extra syntax for defining data types which also automatically creates some field accessor functions.
301+
302+
We can write the following:
303+
304+
::
305+
306+
data TyType =
307+
Constructor { field1 :: Int
308+
, field2 :: String
309+
}
310+
311+
This defines the type the same way as the other ``data`` construct.
312+
Meaning we can pattern match as usual on the constructor.
313+
314+
315+
::
316+
317+
data MyType = Constructor Int String
318+
319+
let theData = Constructor 9 "hello" :: MyType
320+
let theInt = case theData of
321+
Constructor i _ -> i
322+
323+
theInt == 9
324+
325+
But additionally it also defines two functions ``field1`` and ``field2`` for accessing the fields.
326+
327+
Aka it generates code similar to the following:
328+
329+
::
330+
331+
data MyType = Constructor Int String
332+
333+
field1 :: MyType -> Int
334+
field1 (Constructor i _) = i
335+
336+
field2 :: MyType -> String
337+
field2 (Constructor _ s) = s
338+
339+
Also the two accessor functions ``field1`` and ``field2`` may be used in a special *record update syntax* to create a new record from an old one with altered field contents.
340+
Additionally the record may be created with a special record creation syntax.
341+
342+
::
343+
344+
data TyType =
345+
Constructor { field1 :: Int
346+
, field2 :: String
347+
}
348+
349+
let v1 = Constructor 9 "Hello" :: MyType
350+
351+
-- record creation syntax
352+
let v2 = Constructor { field2 = "World", field1 = 4 } :: MyType
353+
354+
-- update syntax
355+
let v3 = v2 { field1 = 9 }
356+
-- updating multiple fields at once
357+
let v4 = v2 { field1 = 9, field2 "Hello" }
358+
359+
v1 == v4
360+
361+
-- old records are unchanged
362+
v2 /= v3 /= v4
299363

300364

0 commit comments

Comments
 (0)