Skip to content

Commit 7ca692b

Browse files
committed
Added IO lesson and slides
1 parent 894a3f0 commit 7ca692b

23 files changed

Lines changed: 927 additions & 116 deletions

Watch.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ reloadChrome = "tell application \"Google Chrome\"\n\
2525
\ end tell\n\
2626
\end tell\n\
2727
\\n\
28-
\tell application \"Code\" to activate\n\
28+
\tell application \"Sublime Text\" to activate\n\
2929
\"
3030

3131
main = do

docs/script.pdf

7.6 KB
Binary file not shown.

docs/script/_sources/exercises/index.rst.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ Exercises
1111
basics
1212
custom-boolean
1313
safe-html
14+
io
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Simple I/O
2+
==========
3+
4+
Reverser
5+
--------
6+
7+
Write an application which reads a line from stdin and prints the same string back, but reversed.
8+
9+
Modify it so it keeps repeating this process forever.
10+
11+
Bulk rename
12+
-----------
13+
14+
You will need the `filepath`_ and `directory`_ library.
15+
16+
Implement a function which:
17+
18+
- takes as argument a string
19+
- then scans the current directory and finds all files
20+
- renames each file by prepending the string argument to the filename
21+
22+
.. _filepath: https://www.stackage.org/lts-8.13/package/filepath-1.4.1.1
23+
.. _directory: https://www.stackage.org/lts-8.13/package/directory-1.3.0.
24+
25+
Advanced
26+
^^^^^^^^
27+
28+
Get the input string from the application arguments [#getArgs]_.
29+
30+
Combine files
31+
-------------
32+
33+
You will need the `filepath`_ and `directory`_ library.
34+
35+
Implement a function which:
36+
37+
- Scans the current directory
38+
- finds all files
39+
- reads each file and collects the contents in a list
40+
- writes a combined output file with the contents of each of the files concatenated.
41+
42+
Advanced
43+
^^^^^^^^
44+
45+
Instead of collecting to a list open the output file first and write each files contents to the output handle right away.
46+
47+
.. rubric:: footnotes
48+
49+
.. [#getArgs] Use the `getArgs <https://www.stackage.org/haddock/lts-8.13/base-4.9.1.0/System-Environment.html#v:getArgs>`__ function.
50+

docs/script/_sources/index.rst.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Some links to motivational Haskell source files:
2020
types
2121
modules
2222
typeclasses
23-
simple-io
23+
io
2424
exercises/index
2525

2626

docs/script/_sources/io.rst.txt

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
I/O and ``do`` notation
2+
=======================
3+
4+
The Haskell language is very self contained due to its pure nature.
5+
Consecutive calls to a function with the same input *has* to produce the same result.
6+
This does not allow for interactions with the stateful environment, like accessing the hard disk, network or database.
7+
8+
To separate these stateful actions from the pure Haskell has a type called ``IO``.
9+
The ``IO`` type is used to tag functions and values.
10+
For instance ``IO Int`` means that we can obtain an ``Int`` from this value if we let it execute some interaction with the environment.
11+
12+
A very common type is ``IO ()`` this means the function does I/O and then returns the ``()`` (unit) value.
13+
This value contains no information (similar to ``null``) and ``IO ()`` is basically equivalent to ``void``.
14+
It marks a function which we only want for its *effect*, not its returned *value*.
15+
16+
An nice example of this is the ``getLine`` function.
17+
As you may imagine it reads a single line from stdin and gives us back what was entered.
18+
Its type ``IO String`` then means that it returns a string after doing some interactions with the environment, in this case reading from the ``stdin`` handle.
19+
20+
``do`` ing IO
21+
-------------
22+
23+
IO actions can be chained using the ``do`` syntax.
24+
``do`` syntax is basically what every function body in an imperative language is, a series of statemens and assignments.
25+
One important thing to note is that all statements in a ``do`` block are executed in sequence.
26+
27+
::
28+
29+
main = do
30+
putStrLn "Starting work"
31+
writeFile "Output" "work work work"
32+
putStrLn "Finished work"
33+
34+
As you can see we can use ``do`` to execute several ``IO`` actions in sequence.
35+
We can also obtain the values in from inside those tagged with ``IO``.
36+
37+
::
38+
39+
main = do
40+
l <- getLine
41+
putStrLn ("You entered the line: " ++ l)
42+
43+
The ``binding <- ioExpr`` syntax means "execute the I/O from ``ioExpr`` and bind the result to ``binding``".
44+
Since ``<-`` is only for ``IO`` tagged values you cannot use it for pure ones.
45+
To handle pure values use the statement for of ``let``: ``let binding = expr`` (notice no ``in``).
46+
47+
::
48+
49+
action :: IO ()
50+
actions = do
51+
l <- getLine
52+
let computed = computeStruff l
53+
return computed
54+
55+
The ``do`` syntax does however not actualy execute the ``IO``.
56+
It merely combines several ``IO`` actions into a larger ``IO`` action.
57+
The value from the last statement in a ``do`` block is what the whole thing returns.
58+
For instance if the last statement was `putStrLn "some string"` the type of the whole block would be ``IO ()`` (void).
59+
If it was ``getLine`` the type would be ``IO String``.
60+
You can also return non-I/O values from within ``do`` by tagging them with ``IO`` using the ``return`` funciton.
61+
62+
63+
64+
Running ``IO``
65+
--------------
66+
67+
To execute the action there are two ways.
68+
69+
#. GHCi
70+
If you type an ``IO`` action into ghci it will execute it for you and print the returned value.
71+
72+
#. The ``main`` function.
73+
74+
When you compile and run a Haskell program or interactively run a Haskell source file the compiler will search for a ``main`` function of type ``IO ()`` and execute all the I/O inside it.
75+
This means you must tie all the I/O you want to do somehow back to the main function.
76+
This is similar to a C program for instance where the ``int main()`` function is the only one automatically executed and all other routines have to be called from within it.
77+
78+
``do`` Overload
79+
---------------
80+
81+
There are more container and tag types which can be used similar to ``IO``.
82+
To be more precise they can be used with the ``do`` notation, just like ``IO`` can.
83+
84+
Examples of such structures are ``[a]``, ``Maybe a``, ``State s a`` and ``Reader e a``.
85+
Like ``IO`` all these structures represent some kind of context for the contained value ``a``.
86+
87+
We will explore this in more detail later.
88+
89+
For now it suffices that in Haskell these structures are generalized with a typeclass called ``Monad``.
90+
The ``Monad m`` typeclass requires two capabilities: ``return :: a -> m a`` to wrap a value ``a`` into the monad ``m`` and bind ``(>>=) :: m a -> (a -> m b) -> m b`` which basically states that the computations with context (the *Monad*) can be chained.
91+
92+
This is all that is necessary to enable them to use the ``do`` notation.
93+
94+
There is a nice library called `monad-loops`_ which implements many of the control structures one is used to from imperative languages in terms of ``Monad``.
95+
96+
.. _monad-loops: https://www.stackage.org/haddock/lts-8.13/monad-loops-0.4.3/Control-Monad-Loops.html
97+
98+
Also of interest should be the `Control.Monad <base_monad>`_ module from the base library which also contains some generic interactions for monads.
99+
For now it is enough to know that functions with the ``:: Monad m =>`` requirement can be used with ``IO``.
100+
101+
.. _base_monad: https://www.stackage.org/haddock/lts-8.13/base-4.9.1.0/Control-Monad.html

docs/script/_sources/simple-io.rst.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/script/exercises/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<link rel="index" title="Index" href="../genindex.html" />
2828
<link rel="search" title="Search" href="../search.html" />
2929
<link rel="next" title="8.1. Tools" href="tools.html" />
30-
<link rel="prev" title="7. IO basics" href="../simple-io.html" />
30+
<link rel="prev" title="7. I/O and do notation" href="../io.html" />
3131

3232
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
3333

@@ -52,6 +52,7 @@ <h1>8. Exercises<a class="headerlink" href="#exercises" title="Permalink to this
5252
<li class="toctree-l1"><a class="reference internal" href="basics.html">8.2. Basics</a></li>
5353
<li class="toctree-l1"><a class="reference internal" href="custom-boolean.html">8.3. A custom boolean</a></li>
5454
<li class="toctree-l1"><a class="reference internal" href="safe-html.html">8.4. Implementing a library for safe html construction</a></li>
55+
<li class="toctree-l1"><a class="reference internal" href="io.html">8.5. Simple I/O</a></li>
5556
</ul>
5657
</div>
5758
</div>
@@ -65,7 +66,7 @@ <h1>8. Exercises<a class="headerlink" href="#exercises" title="Permalink to this
6566
<h3>Related Topics</h3>
6667
<ul>
6768
<li><a href="../index.html">Documentation overview</a><ul>
68-
<li>Previous: <a href="../simple-io.html" title="previous chapter">7. IO basics</a></li>
69+
<li>Previous: <a href="../io.html" title="previous chapter">7. I/O and <code class="docutils literal"><span class="pre">do</span></code> notation</a></li>
6970
<li>Next: <a href="tools.html" title="next chapter">8.1. Tools</a></li>
7071
</ul></li>
7172
</ul>

docs/script/exercises/io.html

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3+
4+
5+
<html xmlns="http://www.w3.org/1999/xhtml">
6+
<head>
7+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8+
9+
<title>8.5. Simple I/O &#8212; Haskell Lessons 0.1 documentation</title>
10+
11+
<link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
12+
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13+
14+
<script type="text/javascript">
15+
var DOCUMENTATION_OPTIONS = {
16+
URL_ROOT: '../',
17+
VERSION: '0.1',
18+
COLLAPSE_INDEX: false,
19+
FILE_SUFFIX: '.html',
20+
HAS_SOURCE: true,
21+
SOURCELINK_SUFFIX: '.txt'
22+
};
23+
</script>
24+
<script type="text/javascript" src="../_static/jquery.js"></script>
25+
<script type="text/javascript" src="../_static/underscore.js"></script>
26+
<script type="text/javascript" src="../_static/doctools.js"></script>
27+
<link rel="index" title="Index" href="../genindex.html" />
28+
<link rel="search" title="Search" href="../search.html" />
29+
<link rel="prev" title="8.4. Implementing a library for safe html construction" href="safe-html.html" />
30+
31+
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
32+
33+
34+
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
35+
36+
</head>
37+
<body role="document">
38+
39+
40+
<div class="document">
41+
<div class="documentwrapper">
42+
<div class="bodywrapper">
43+
<div class="body" role="main">
44+
45+
<div class="section" id="simple-i-o">
46+
<h1>8.5. Simple I/O<a class="headerlink" href="#simple-i-o" title="Permalink to this headline"></a></h1>
47+
<div class="section" id="reverser">
48+
<h2>8.5.1. Reverser<a class="headerlink" href="#reverser" title="Permalink to this headline"></a></h2>
49+
<p>Write an application which reads a line from stdin and prints the same string back, but reversed.</p>
50+
<p>Modify it so it keeps repeating this process forever.</p>
51+
</div>
52+
<div class="section" id="bulk-rename">
53+
<h2>8.5.2. Bulk rename<a class="headerlink" href="#bulk-rename" title="Permalink to this headline"></a></h2>
54+
<p>You will need the <a class="reference external" href="https://www.stackage.org/lts-8.13/package/filepath-1.4.1.1">filepath</a> and <a class="reference external" href="https://www.stackage.org/lts-8.13/package/directory-1.3.0.">directory</a> library.</p>
55+
<p>Implement a function which:</p>
56+
<ul class="simple">
57+
<li>takes as argument a string</li>
58+
<li>then scans the current directory and finds all files</li>
59+
<li>renames each file by prepending the string argument to the filename</li>
60+
</ul>
61+
<div class="section" id="advanced">
62+
<h3>8.5.2.1. Advanced<a class="headerlink" href="#advanced" title="Permalink to this headline"></a></h3>
63+
<p>Get the input string from the application arguments <a class="footnote-reference" href="#getargs" id="id1">[1]</a>.</p>
64+
</div>
65+
</div>
66+
<div class="section" id="combine-files">
67+
<h2>8.5.3. Combine files<a class="headerlink" href="#combine-files" title="Permalink to this headline"></a></h2>
68+
<p>You will need the <a class="reference external" href="https://www.stackage.org/lts-8.13/package/filepath-1.4.1.1">filepath</a> and <a class="reference external" href="https://www.stackage.org/lts-8.13/package/directory-1.3.0.">directory</a> library.</p>
69+
<p>Implement a function which:</p>
70+
<ul class="simple">
71+
<li>Scans the current directory</li>
72+
<li>finds all files</li>
73+
<li>reads each file and collects the contents in a list</li>
74+
<li>writes a combined output file with the contents of each of the files concatenated.</li>
75+
</ul>
76+
<div class="section" id="id2">
77+
<h3>8.5.3.1. Advanced<a class="headerlink" href="#id2" title="Permalink to this headline"></a></h3>
78+
<p>Instead of collecting to a list open the output file first and write each files contents to the output handle right away.</p>
79+
<p class="rubric">footnotes</p>
80+
<table class="docutils footnote" frame="void" id="getargs" rules="none">
81+
<colgroup><col class="label" /><col /></colgroup>
82+
<tbody valign="top">
83+
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>Use the <a class="reference external" href="https://www.stackage.org/haddock/lts-8.13/base-4.9.1.0/System-Environment.html#v:getArgs">getArgs</a> function.</td></tr>
84+
</tbody>
85+
</table>
86+
</div>
87+
</div>
88+
</div>
89+
90+
91+
</div>
92+
</div>
93+
</div>
94+
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
95+
<div class="sphinxsidebarwrapper">
96+
<h3><a href="../index.html">Table Of Contents</a></h3>
97+
<ul>
98+
<li><a class="reference internal" href="#">8.5. Simple I/O</a><ul>
99+
<li><a class="reference internal" href="#reverser">8.5.1. Reverser</a></li>
100+
<li><a class="reference internal" href="#bulk-rename">8.5.2. Bulk rename</a><ul>
101+
<li><a class="reference internal" href="#advanced">8.5.2.1. Advanced</a></li>
102+
</ul>
103+
</li>
104+
<li><a class="reference internal" href="#combine-files">8.5.3. Combine files</a><ul>
105+
<li><a class="reference internal" href="#id2">8.5.3.1. Advanced</a></li>
106+
</ul>
107+
</li>
108+
</ul>
109+
</li>
110+
</ul>
111+
<div class="relations">
112+
<h3>Related Topics</h3>
113+
<ul>
114+
<li><a href="../index.html">Documentation overview</a><ul>
115+
<li><a href="index.html">8. Exercises</a><ul>
116+
<li>Previous: <a href="safe-html.html" title="previous chapter">8.4. Implementing a library for safe html construction</a></li>
117+
</ul></li>
118+
</ul></li>
119+
</ul>
120+
</div>
121+
<div role="note" aria-label="source link">
122+
<h3>This Page</h3>
123+
<ul class="this-page-menu">
124+
<li><a href="../_sources/exercises/io.rst.txt"
125+
rel="nofollow">Show Source</a></li>
126+
</ul>
127+
</div>
128+
<div id="searchbox" style="display: none" role="search">
129+
<h3>Quick search</h3>
130+
<form class="search" action="../search.html" method="get">
131+
<div><input type="text" name="q" /></div>
132+
<div><input type="submit" value="Go" /></div>
133+
<input type="hidden" name="check_keywords" value="yes" />
134+
<input type="hidden" name="area" value="default" />
135+
</form>
136+
</div>
137+
<script type="text/javascript">$('#searchbox').show(0);</script>
138+
</div>
139+
</div>
140+
<div class="clearer"></div>
141+
</div>
142+
<div class="footer">
143+
&copy;2017, Justus Adam.
144+
145+
|
146+
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.5.1</a>
147+
&amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.9</a>
148+
149+
|
150+
<a href="../_sources/exercises/io.rst.txt"
151+
rel="nofollow">Page source</a>
152+
</div>
153+
154+
155+
156+
157+
</body>
158+
</html>

docs/script/exercises/safe-html.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<script type="text/javascript" src="../_static/doctools.js"></script>
2727
<link rel="index" title="Index" href="../genindex.html" />
2828
<link rel="search" title="Search" href="../search.html" />
29+
<link rel="next" title="8.5. Simple I/O" href="io.html" />
2930
<link rel="prev" title="8.3. A custom boolean" href="custom-boolean.html" />
3031

3132
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
@@ -311,6 +312,7 @@ <h3>Related Topics</h3>
311312
<li><a href="../index.html">Documentation overview</a><ul>
312313
<li><a href="index.html">8. Exercises</a><ul>
313314
<li>Previous: <a href="custom-boolean.html" title="previous chapter">8.3. A custom boolean</a></li>
315+
<li>Next: <a href="io.html" title="next chapter">8.5. Simple I/O</a></li>
314316
</ul></li>
315317
</ul></li>
316318
</ul>

0 commit comments

Comments
 (0)