Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updated section on <|>

  • Loading branch information...
commit d5cb2893cdef936b5049c03cb579b707eb3dde1e 1 parent 2a1e444
@mzero authored
Showing with 54 additions and 8 deletions.
  1. +44 −0 Part3b.hs
  2. +2 −2 slides.html
  3. +8 −6 slides.md
View
44 Part3b.hs
@@ -0,0 +1,44 @@
+module Part3b where
+
+import Control.Applicative
+import Data.List
+import Data.Maybe
+
+
+tvShows :: [(Int, String)] -- a list of pairs
+tvShows =
+ [ (1966, "Star Trek")
+ , (1969, "Monty Python's Flying Circus")
+ , (1989, "The Simpsons")
+ ]
+
+showForYear :: Int -> Maybe String
+showForYear y = lookup y tvShows
+ -- lookup "lookup" w/Hoogle: http://www.haskell.org/hoogle/?hoogle=lookup
+
+showWithName :: String -> Maybe String
+showWithName n = (listToMaybe . filter (isInfixOf n) . map snd) tvShows
+ -- for a good exercise, figure out what this does
+ -- look these functions up in Hoogle (just follow the first hit for each)
+
+favoriteShow :: String -> Maybe String
+favoriteShow "Amy" = Just "Batman"
+favoriteShow "Bob" = Just "Iron Chef"
+favoriteShow _ = Nothing
+
+
+data Person = Person { name :: String, year :: Int }
+ -- This has "named" fields, which act as accessor functions
+
+amy = Person { name = "Amy", year = 1971 }
+cam = Person { name = "Cam", year = 1989 }
+deb = Person { name = "Deb", year = 1967 }
+monty = Person { name = "Monty", year = 1973 }
+
+pickShow :: Person -> Maybe String
+pickShow p =
+ favoriteShow (name p)
+ <|> showWithName (name p)
+ <|> showForYear (year p)
+
+
View
4 slides.html
@@ -696,9 +696,9 @@ <h1 id="thinking-of-fmap-like-a-functional-programmer">Thinking of <code>fmap</c
</div>
<div class="slide">
<h1 id="power-alternatives">Power alternatives: <code>&lt;|&gt;</code></h1>
-<pre class="sourceCode"><code class="sourceCode haskell">locTimeZone shippingLocation<br /> <span class="fu">&lt;|&gt;</span> userTimeZone preferences<br /> <span class="fu">&lt;|&gt;</span> requestTimeZone request</code></pre>
+<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">pickShow </span><span class="ot">::</span> <span class="dt">Person</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">String</span><br />pickShow p <span class="fu">=</span><br /> favoriteShow (name p)<br /> <span class="fu">&lt;|&gt;</span> showWithName (name p)<br /> <span class="fu">&lt;|&gt;</span> showForYear (year p)</code></pre>
<p>Given:</p>
-<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">locTimeZone </span><span class="ot">::</span> <span class="dt">Location</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">TimeZone</span><br /><br /><span class="ot">userTimeZone </span><span class="ot">::</span> <span class="dt">Preferences</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">TimeZone</span><br /><br /><span class="ot">requestTimeZone </span><span class="ot">::</span> <span class="dt">HttpRequest</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">TimeZone</span></code></pre>
+<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">favoriteShow </span><span class="ot">::</span> <span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">String</span><br /><br /><span class="ot">showWithName </span><span class="ot">::</span> <span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">String</span><br /><br /><span class="ot">showForYear </span><span class="ot">::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">String</span></code></pre>
<p>Like short circuit due to lazy evaluation</p>
</div>
<div class="slide">
View
14 slides.md
@@ -507,19 +507,21 @@ aWeekLater' = maybeAddAWeek anInterestingDate
# Power alternatives: `<|>`
~~~~ {.haskell}
-locTimeZone shippingLocation
- <|> userTimeZone preferences
- <|> requestTimeZone request
+pickShow :: Person -> Maybe String
+pickShow p =
+ favoriteShow (name p)
+ <|> showWithName (name p)
+ <|> showForYear (year p)
~~~~
Given:
~~~~ {.haskell}
-locTimeZone :: Location -> Maybe TimeZone
+favoriteShow :: String -> Maybe String
-userTimeZone :: Preferences -> Maybe TimeZone
+showWithName :: String -> Maybe String
-requestTimeZone :: HttpRequest -> Maybe TimeZone
+showForYear :: Int -> Maybe String
~~~~
Like short circuit due to lazy evaluation
Please sign in to comment.
Something went wrong with that request. Please try again.