-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathLessons-Lesson09.html
More file actions
40 lines (40 loc) · 11 KB
/
Lessons-Lesson09.html
File metadata and controls
40 lines (40 loc) · 11 KB
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><title>Lessons.Lesson09</title><link href="linuwial.css" rel="stylesheet" type="text/css" title="Linuwial" /><link rel="stylesheet" type="text/css" href="quick-jump.css" /><link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=PT+Sans:400,400i,700" /><script src="haddock-bundle.min.js" async="async" type="text/javascript"></script><script type="text/x-mathjax-config">MathJax.Hub.Config({ tex2jax: { processClass: "mathjax", ignoreClass: ".*" } });</script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script></head><body><div id="package-header"><span class="caption empty"> </span><ul class="links" id="page-menu"><li><a href="src/Lessons.Lesson09.html">Source</a></li><li><a href="index.html">Contents</a></li><li><a href="doc-index.html">Index</a></li></ul></div><div id="content"><div id="module-header"><table class="info"><tr><th>Safe Haskell</th><td>Safe-Inferred</td></tr></table><p class="caption">Lessons.Lesson09</p></div><div id="table-of-contents"><div id="contents-list"><p class="caption" onclick="window.scrollTo(0,0)">Contents</p><ul><li><a href="#section.orphans">Orphan instances</a></li></ul></div></div><div id="description"><p class="caption">Description</p><div class="doc"><p>Notes taken by Julija Mikeliūnaitė</p></div></div><div id="synopsis"><details id="syn"><summary>Synopsis</summary><ul class="details-toggle" data-details-id="syn"><li class="src short"><a href="#v:queryAge">queryAge</a> :: String -> IO Integer</li><li class="src short"><a href="#v:threeThreeLetterWords">threeThreeLetterWords</a> :: <a href="Lessons-Lesson08.html#t:Parser" title="Lessons.Lesson08">Parser</a> [String]</li><li class="src short"><a href="#v:stateful">stateful</a> :: State String Int</li><li class="src short"><a href="#v:combined">combined</a> :: State String (Int, Int)</li><li class="src short"><a href="#v:combined-39-">combined'</a> :: State String (Int, Int)</li><li class="src short"><a href="#v:combined-39--39-">combined''</a> :: State String (Int, Int)</li></ul></details></div><div id="interface"><h1>Documentation</h1><div class="top"><p class="src"><a id="v:queryAge" class="def">queryAge</a> :: String -> IO Integer <a href="src/Lessons.Lesson09.html#queryAge" class="link">Source</a> <a href="#v:queryAge" class="selflink">#</a></p><div class="doc"><p>Take a list as a traversable, pass a list of Maybies. Returns a Maybe containing a list.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>sequenceA [Just 42, Just 5]
</code></strong>Just [42,5]
</pre><p>If the passed list contains a Nothing, Nothing is returned.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>sequenceA [Just 42, Just 5, Nothing]
</code></strong>Nothing
</pre><p>Works pretty much as list comprehension.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>sequenceA [[42], [13, 45]]
</code></strong>[[42,13],[42,45]]
</pre><p>Returns empty list.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>sequenceA [[42], [13, 45], []]
</code></strong>[]
</pre><p>Because a Left value was passed, it also returns a Left value.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>sequenceA [Left 0, Right 43, Right 45]
</code></strong>Left 0
</pre><p>Returns the inner value (Left 45).</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>sequenceA $ Right (Left 45)
</code></strong>Left 45
</pre><p>Swaps the traversable with applicative.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>sequenceA $ Just (Right 45)
</code></strong>Right (Just 45)
</pre><p>Collects all the inputs. Inner values are Just monadic values, type is IO [String]. Result is an IO list of pure values, which makes life easier.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>:t sequenceA [getLine, getLine, getLine]
</code></strong>sequenceA [getLine, getLine, getLine] :: IO [String]
</pre><p>Takes pure value (name) and returns a monadic value.</p></div></div><div class="top"><p class="src"><a id="v:threeThreeLetterWords" class="def">threeThreeLetterWords</a> :: <a href="Lessons-Lesson08.html#t:Parser" title="Lessons.Lesson08">Parser</a> [String] <a href="src/Lessons.Lesson09.html#threeThreeLetterWords" class="link">Source</a> <a href="#v:threeThreeLetterWords" class="selflink">#</a></p></div><div class="top"><p class="src"><a id="v:stateful" class="def">stateful</a> :: State String Int <a href="src/Lessons.Lesson09.html#stateful" class="link">Source</a> <a href="#v:stateful" class="selflink">#</a></p><div class="doc"><p>State monad - it tries to pretend that Haskell is a normal programming language with a mutable state.
| State has two params: String - type of state, and Int - type of computation result. get - gets global state, put - changes global state. In this case, global is super local.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>runState stateful "initial"
</code></strong>(7,"I am a new state")
</pre></div></div><div class="top"><p class="src"><a id="v:combined" class="def">combined</a> :: State String (Int, Int) <a href="src/Lessons.Lesson09.html#combined" class="link">Source</a> <a href="#v:combined" class="selflink">#</a></p><div class="doc"><p>7 is the old state length, 16 is the new one.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>runState combined "initial"
</code></strong>((7,16),"I am a new state")
</pre></div></div><div class="top"><p class="src"><a id="v:combined-39-" class="def">combined'</a> :: State String (Int, Int) <a href="src/Lessons.Lesson09.html#combined%27" class="link">Source</a> <a href="#v:combined-39-" class="selflink">#</a></p></div><div class="top"><p class="src"><a id="v:combined-39--39-" class="def">combined''</a> :: State String (Int, Int) <a href="src/Lessons.Lesson09.html#combined%27%27" class="link">Source</a> <a href="#v:combined-39--39-" class="selflink">#</a></p></div><h1>Orphan instances</h1><div id="section.orphans"><table><tr><td class="src clearfix"><span class="inst-left"><span class="instance details-toggle-control details-toggle" data-details-id="i:o:ic:Monad:Monad:1"></span> Monad <a href="Lessons-Lesson08.html#t:Parser" title="Lessons.Lesson08">Parser</a></span> <a href="src/Lessons.Lesson09.html#line-132" class="link">Source</a> <a href="#v:-36-fMonadParser" class="selflink">#</a></td><td class="doc"><p>If a list of names is provided, it returns a list of numbers (type IO [Integer]).</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>:t mapM queryAge ["VI", "A", "U"]
</code></strong>mapM queryAge ["VI", "A", "U"] :: IO [Integer]
</pre><p>Return type is [IO Integer] (not IO [Integer]).</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>:t map queryAge ["VI", "A", "U"]
</code></strong>map queryAge ["VI", "A", "U"] :: [IO Integer]
</pre><p>Usually functions with _'s drop the final result and only run side-effects. Running a bunch of computations when you don't really care about the result.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>:t mapM_ queryAge ["VI", "A", "U"]
</code></strong>mapM_ queryAge ["VI", "A", "U"] :: IO ()
</pre><p>Closest thing Haskell has to loop (similar to <code>foreach</code> in other languages). Usually used for running a computation over some collection. Works the same way as mapM_ but has swapped arguments.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>:t forM_ ["VI", "A", "U"] queryAge
</code></strong>forM_ ["VI", "A", "U"] queryAge :: IO ()
</pre><p>The number next to M shows how many arguments the first function takes. Works as (<a href="$">$</a>).</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>liftM2 (+) (Just 4) (Just 6)
</code></strong>Just 10
</pre><p>Lifts a pure function into a monadic function.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>liftM3 (\a b c -> a + b - c) (Just 4) (Just 6) Nothing
</code></strong>Nothing
</pre><p>Same result as before.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>liftA3 (\a b c -> a + b - c) (Just 4) (Just 6) Nothing
</code></strong>Nothing
</pre><p>Using our helpers in the context of parsers (making them useful).</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>:t threeLetters
</code></strong>threeLetters :: Parser String
</pre><p>After the first parser completes its course, the second parser continues with the unparsed string.</p><pre class="screen"><code class="prompt">>>> </code><strong class="userinput"><code>runParser (sequenceA [threeLetters, threeLetters, threeLetters]) "asdasdasd!"
</code></strong>Right (["asd","asd","asd"],"!")
</pre><p>Pretty much every program is just a traversable.</p><p>Allows to write parser combinators in a monadic way.</p></td></tr><tr><td colspan="2"><details id="i:o:ic:Monad:Monad:1"><summary class="hide-when-js-enabled">Instance details</summary><p></p> <div class="subs methods"><p class="caption">Methods</p><p class="src"><a href="#v:-62--62--61-">(>>=)</a> :: <a href="Lessons-Lesson08.html#t:Parser" title="Lessons.Lesson08">Parser</a> a -> (a -> <a href="Lessons-Lesson08.html#t:Parser" title="Lessons.Lesson08">Parser</a> b) -> <a href="Lessons-Lesson08.html#t:Parser" title="Lessons.Lesson08">Parser</a> b</p><p class="src"><a href="#v:-62--62-">(>>)</a> :: <a href="Lessons-Lesson08.html#t:Parser" title="Lessons.Lesson08">Parser</a> a -> <a href="Lessons-Lesson08.html#t:Parser" title="Lessons.Lesson08">Parser</a> b -> <a href="Lessons-Lesson08.html#t:Parser" title="Lessons.Lesson08">Parser</a> b</p><p class="src"><a href="#v:return">return</a> :: a -> <a href="Lessons-Lesson08.html#t:Parser" title="Lessons.Lesson08">Parser</a> a</p></div></details></td></tr></table></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.29.2</p></div></body></html>