Skip to content

Commit

Permalink
move initial Maybe discussion up a bit, added slide on Type classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mzero committed Oct 14, 2011
1 parent caabd4e commit fdfddab
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 31 deletions.
5 changes: 5 additions & 0 deletions Part2d.hs
@@ -1,5 +1,10 @@
module Part2d where

pickMessage :: Maybe Int -> String
pickMessage (Just n) = "Pick a number, like " ++ show n ++ "."
pickMessage Nothing = "Pick any number you like."


findAfterStar :: String -> Maybe Char
findAfterStar (c:d:r) =
if c == '*' then Just d
Expand Down
36 changes: 25 additions & 11 deletions slides.html
Expand Up @@ -220,8 +220,19 @@ <h1 id="actually-not-even-that-much">Actually, not even that much</h1>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="kw">data</span> [] a <span class="fu">=</span> [] <span class="fu">|</span> a <span class="fu">:</span> [a] <span class="co">-- this is in the standard library</span><br /><span class="kw">infixr</span> <span class="dv">5</span> <span class="fu">:</span><br /><br />empty <span class="fu">=</span> []<br />oneWord <span class="fu">=</span> [<span class="st">&quot;apple&quot;</span>] <span class="co">-- syntatic sugar</span><br />twoWords <span class="fu">=</span> [<span class="st">&quot;banana&quot;</span>, <span class="st">&quot;cantaloupe&quot;</span>] <span class="co">-- two teaspoons full</span><br /><br />mystery1 <span class="fu">=</span> <span class="st">&quot;pear&quot;</span> <span class="fu">:</span> empty<br />mystery2 <span class="fu">=</span> <span class="st">&quot;peach&quot;</span> <span class="fu">:</span> oneWord<br />mystery3 <span class="fu">=</span> <span class="st">&quot;pineapple&quot;</span> <span class="fu">:</span> mystery3<br />mystery4 <span class="fu">=</span> [<span class="dv">42</span>, <span class="st">&quot;apple&quot;</span>] <span class="co">-- sweet, but still won't compile</span><br /><br /><span class="ot">dropOne </span><span class="ot">::</span> [a] <span class="ot">-&gt;</span> [a]<br />dropOne (first<span class="fu">:</span>rest) <span class="fu">=</span> rest<br />dropOne [] <span class="fu">=</span> []<br /><br /><span class="ot">justOne </span><span class="ot">::</span> [a] <span class="ot">-&gt;</span> [a] <span class="co">-- don't confuse these &quot;[a]&quot;s</span><br />justOne (a<span class="fu">:</span>_) <span class="fu">=</span> [a] <span class="co">-- with this &quot;[a]&quot;</span><br />justOne [] <span class="fu">=</span> []</code></pre>
</div>
<div class="slide">
<h1 id="one-more-standard-thing">One more standard thing:</h1>
<h1 id="two-more-standard-things">Two more standard things:</h1>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="kw">type</span> <span class="dt">String</span> <span class="fu">=</span> [<span class="dt">Char</span>]</code></pre>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="kw">data</span> <span class="dt">Maybe</span> a <span class="fu">=</span> <span class="kw">Nothing</span> <span class="fu">|</span> <span class="kw">Just</span> a</code></pre>
</div>
<div class="slide">
<h1 id="a-bad-function">A bad function</h1>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">firstOne </span><span class="ot">::</span> [a] <span class="ot">-&gt;</span> a<br />firstOne (a<span class="fu">:</span>_) <span class="fu">=</span> a<br />firstOne [] <span class="fu">=</span> <span class="fu">error</span> <span class="st">&quot;O Noes!&quot;</span></code></pre>
</div>
<div class="slide">
<h1 id="maybe-a-better-way">Maybe a better way</h1>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">firstOne' </span><span class="ot">::</span> [a] <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a<br />firstOne' (a<span class="fu">:</span>_) <span class="fu">=</span> <span class="kw">Just</span> a<br />firstOne' [] <span class="fu">=</span> <span class="kw">Nothing</span></code></pre>
<p>Use it like this:</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">pickMessage </span><span class="ot">::</span> <span class="dt">Maybe</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">String</span><br />pickMessage (<span class="kw">Just</span> n) <span class="fu">=</span> <span class="st">&quot;Pick a number, like &quot;</span> <span class="fu">++</span> <span class="fu">show</span> n <span class="fu">++</span> <span class="st">&quot;.&quot;</span><br />pickMessage <span class="kw">Nothing</span> <span class="fu">=</span> <span class="st">&quot;Pick any number you like.&quot;</span></code></pre>
</div>
<div class="slide">
<h1 id="now-lets-write-some-real-code">Now, let's write some real code</h1>
Expand Down Expand Up @@ -249,21 +260,13 @@ <h1 id="the-type-that-blew-my-mind">The type that blew my mind</h1>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="kw">data</span> <span class="dt">Maybe</span> a <span class="fu">=</span> <span class="kw">Nothing</span> <span class="fu">|</span> <span class="kw">Just</span> a</code></pre>
</div>
<div class="slide">
<h1 id="a-bad-function">A bad function</h1>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">firstOne </span><span class="ot">::</span> [a] <span class="ot">-&gt;</span> a<br />firstOne (a<span class="fu">:</span>_) <span class="fu">=</span> a<br />firstOne [] <span class="fu">=</span> <span class="fu">error</span> <span class="st">&quot;O Noes!&quot;</span></code></pre>
</div>
<div class="slide">
<h1 id="maybe-a-better-way">Maybe a better way</h1>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">firstOne' </span><span class="ot">::</span> [a] <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a<br />firstOne' (a<span class="fu">:</span>_) <span class="fu">=</span> <span class="kw">Just</span> a<br />firstOne' [] <span class="fu">=</span> <span class="kw">Nothing</span></code></pre>
</div>
<div class="slide">
<h1 id="useful">Useful!</h1>
<h1 id="maybe-quite-useful"><code>Maybe</code> quite useful:</h1>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">elemIndex </span><span class="ot">::</span> a <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">Int</span><br /><br /><span class="fu">lookup</span><span class="ot"> </span><span class="ot">::</span> k <span class="ot">-&gt;</span> <span class="dt">Map</span> k a <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a<br /><br /><span class="ot">stripPrefix </span><span class="ot">::</span> <span class="dt">Text</span> <span class="ot">-&gt;</span> <span class="dt">Text</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">Text</span><br /><br /><span class="ot">port </span><span class="ot">::</span> <span class="dt">URIAuthority</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">Int</span></code></pre>
</div>
<div class="slide">
<h1 id="power-lifting-fmap">Power lifting: <code>fmap</code></h1>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">addAWeek </span><span class="ot">::</span> <span class="dt">Day</span> <span class="ot">-&gt;</span> <span class="dt">Day</span><br />addAWeek d <span class="fu">=</span> addDays <span class="dv">7</span> d<br /><br /><span class="ot">interestingDates </span><span class="ot">::</span> [<span class="dt">Day</span>]<br />interestingDates <span class="fu">=</span> <span class="fu">...</span><br /><br /><span class="ot">anInterestingDate </span><span class="ot">::</span> <span class="dt">Maybe</span> <span class="dt">Day</span><br />anInterestingDate <span class="fu">=</span> firstOne' interestingDates<br /><br /><span class="ot">aWeekLater </span><span class="ot">::</span> <span class="dt">Maybe</span> <span class="dt">Day</span><br />aWeekLater <span class="fu">=</span> <span class="fu">fmap</span> addAWeek anInterestingDate</code></pre>
<p>See the source for some intersting dates.</p>
<p><em>(See the source for some intersting dates.)</em></p>
</div>
<div class="slide">
<h1 id="thinking-like-a-haskeller">Thinking like a Haskeller</h1>
Expand All @@ -284,6 +287,17 @@ <h1 id="power-injection">Power injection: <code>&gt;&gt;=</code></h1>
<p><code>&gt;&gt;=</code> is actually pronounced &quot;bind&quot;</p>
</div>
<div class="slide">
<h1 id="more-generic">More generic</h1>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="fu">fmap</span><span class="ot"> </span><span class="ot">::</span> <span class="kw">Functor</span> f <span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> f a <span class="ot">-&gt;</span> f b<br /><br /><span class="ot">(&lt;|&gt;) </span><span class="ot">::</span> <span class="dt">Alternative</span> f <span class="ot">=&gt;</span> f a <span class="ot">-&gt;</span> f a <span class="ot">-&gt;</span> f a<br /><br /><span class="ot">(&gt;&gt;=) </span><span class="ot">::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> m a <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> m b) <span class="ot">-&gt;</span> m b</code></pre>
<p>Type classes and instances:</p>
<pre><code>Functor Maybe, [], (Either a), IO

Alternative Maybe, []

Monad Maybe, [], (Either a), IO
</code></pre>
</div>
<div class="slide">
<p>(Time for just one more?)</p>
</div>
<div class="slide">
Expand Down
73 changes: 53 additions & 20 deletions slides.md
Expand Up @@ -393,12 +393,40 @@ justOne (a:_) = [a] -- with this "[a]"
justOne [] = []
~~~~

# One more standard thing:
# Two more standard things:

~~~~ {.haskell}
type String = [Char]
~~~~

~~~~ {.haskell}
data Maybe a = Nothing | Just a
~~~~

# A bad function

~~~~ {.haskell}
firstOne :: [a] -> a
firstOne (a:_) = a
firstOne [] = error "O Noes!"
~~~~

# Maybe a better way

~~~~ {.haskell}
firstOne' :: [a] -> Maybe a
firstOne' (a:_) = Just a
firstOne' [] = Nothing
~~~~

Use it like this:

~~~~ {.haskell}
pickMessage :: Maybe Int -> String
pickMessage (Just n) = "Pick a number, like " ++ show n ++ "."
pickMessage Nothing = "Pick any number you like."
~~~~

# Now, let's write some real code

Find the first character after a star:
Expand Down Expand Up @@ -444,30 +472,13 @@ findAfterElem _ _ = Nothing
*Onward!*



# The type that blew my mind

~~~~ {.haskell}
data Maybe a = Nothing | Just a
~~~~

# A bad function

~~~~ {.haskell}
firstOne :: [a] -> a
firstOne (a:_) = a
firstOne [] = error "O Noes!"
~~~~

# Maybe a better way

~~~~ {.haskell}
firstOne' :: [a] -> Maybe a
firstOne' (a:_) = Just a
firstOne' [] = Nothing
~~~~

# Useful!
# `Maybe` quite useful:

~~~~ {.haskell}
elemIndex :: a -> [a] -> Maybe Int
Expand Down Expand Up @@ -495,7 +506,7 @@ aWeekLater :: Maybe Day
aWeekLater = fmap addAWeek anInterestingDate
~~~~

See the source for some intersting dates.
_(See the source for some intersting dates.)_

# Thinking like a Haskeller

Expand Down Expand Up @@ -550,6 +561,26 @@ mailboxForDate :: Date -> Maybe Mailbox

`>>=` is actually pronounced "bind"

# More generic

~~~~ {.haskell}
fmap :: Functor f => (a -> b) -> f a -> f b
(<|>) :: Alternative f => f a -> f a -> f a
(>>=) :: Monad m => m a -> (a -> m b) -> m b
~~~~

Type classes and instances:

~~~~
Functor Maybe, [], (Either a), IO
Alternative Maybe, []
Monad Maybe, [], (Either a), IO
~~~~

----

(Time for just one more?)
Expand All @@ -558,6 +589,8 @@ mailboxForDate :: Date -> Maybe Mailbox

*Go!*



# Types you don't type

~~~~ {.haskell}
Expand Down

0 comments on commit fdfddab

Please sign in to comment.