Skip to content

Commit 1c53468

Browse files
author
Yoav
authored
Update input-and-output.html (learnyouahaskell#28)
1 parent f5765b9 commit 1c53468

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

docs/input-and-output.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ <h1>Input and Output</h1>
765765
let (Just action) = lookup command dispatch
766766
action args
767767
</pre>
768-
<p>First, we get the arguments and bind them to <span class="fixed">(command:args)</span>. If you remember your pattern matching, this means that the first argument will get bound to <span class="fixed">command</span> and the rest of them will get bound to <span class="fixed">args</span>. If we call our program like <span class="fixed">todo add todo.txt "Spank the monkey"</span>, <span class="fixed">command</span> will be <span class="fixed">"add"</span> and <span class="fixed">args</span> will be <span class="fixed">["todo.xt", "Spank the monkey"]</span>.</p>
768+
<p>First, we get the arguments and bind them to <span class="fixed">(command:args)</span>. If you remember your pattern matching, this means that the first argument will get bound to <span class="fixed">command</span> and the rest of them will get bound to <span class="fixed">args</span>. If we call our program like <span class="fixed">todo add todo.txt "Spank the monkey"</span>, <span class="fixed">command</span> will be <span class="fixed">"add"</span> and <span class="fixed">args</span> will be <span class="fixed">["todo.txt", "Spank the monkey"]</span>.</p>
769769
<p>In the next line, we look up our command in the dispatch list. Because <span class="fixed">"add"</span> points to <span class="fixed">add</span>, we get <span class="fixed">Just add</span> as a result. We use pattern matching again to extract our function out of the <span class="fixed">Maybe</span>. What happens if our command isn't in the dispatch list? Well then the lookup will return <span class="fixed">Nothing</span>, but we said we won't concern ourselves with failing gracefully too much, so the pattern matching will fail and our program will throw a fit.</p>
770770
<p>Finally, we call our <span class="fixed">action</span> function with the rest of the argument list. That will return an I/O action that either adds an item, displays a list of items or deletes an item and because that action is part of the <span class="fixed">main</span> <i>do</i> block, it will get performed. If we follow our concrete example so far and our <span class="fixed">action</span> function is <span class="fixed">add</span>, it will get called with <span class="fixed">args</span> (so <span class="fixed">["todo.txt", "Spank the monkey"]</span>) and return an I/O action that adds <span class="fixed">Spank the monkey</span> to <i>todo.txt</i>.</p>
771771
<p>Great! All that's left now is to implement <span class="fixed">add</span>, <span class="fixed">view</span> and <span class="fixed">remove</span>. Let's start with <span class="fixed">add</span>:</p>

0 commit comments

Comments
 (0)