Skip to content
Browse files

add some of Chapter 3

  • Loading branch information...
1 parent 0833a2e commit 96feb24d6abbe9c44c2d2b115c3fbb8be5ca8360 @mzero committed Feb 11, 2011
Showing with 190 additions and 2 deletions.
  1. +3 −2 SYLLABUS.markdown
  2. +6 −0 TODO
  3. +39 −0 seed/Chapter3/Step_3_1.hs
  4. +52 −0 seed/Chapter3/Step_3_2.hs
  5. +53 −0 seed/Chapter3/Step_3_3.hs
  6. +32 −0 seed/Chapter3/Step_3_4.hs
  7. +5 −0 seed/lib/Tutorial.hs
View
5 SYLLABUS.markdown
@@ -285,10 +285,11 @@ Chapter Goals
-- version w/higher order functions (map)
-- where clause
-- develop the numbered list in concert with the todo list
+ -- write map
3: todo list w/priority
-- 2-tuple
- -- 3-tuple ->
-- data types
- --
+ -- Maybe
+
View
6 TODO
@@ -29,6 +29,10 @@ How to use this list:
=== BARLEY TODO ===
+--- Required Tasks to get to Workshop ---
+
+[] Next/Prev on slides needs to either be dimmed at end...
+ or take you to next/prev step of the tutorial
--- Required Tasks to get to 0.2 ---
@@ -370,6 +374,8 @@ even I'm confused about when to use finishWith vs. pass
parseHttpTime has no option for failure - should return a Maybe
formatHttpTime & parseHttpTime use CTime? and need IO?
should there be utilities for handling expires times?
+when returning errors w/o content, no way to have not Content-Length
+
=== NOTES FOR HTML ===
renderHtml is BROKEN - it puts things inside an extra HTML element!?!?!?
View
39 seed/Chapter3/Step_3_1.hs
@@ -0,0 +1,39 @@
+module Step_3_1 where
+
+import Slides
+
+page = slideDeck "Chapter 3"
+ [ titleSlide "Chapter 3"
+ [ "Tuples, Types, and Maybe"
+ ]
+ , codeSlide "Two Things"
+ "A 2-Tuple is pair of two values, the fst and the snd \
+ \(not a typo!):"
+ [ "(43, \"Bob\") -- a tuple of an Int and String"
+ , "(\"Bob\", 43) -- a tuple of a String and an Int"
+ , "(True, [1, 2, 3]) -- a tuple of a Bool and a list"
+ , ""
+ , "('x', (True, 12)) -- hmmmmm..."
+ ]
+ , codeSlide "Two Things"
+ "When we talk about the type of a tuple, we must specify \
+ \the type of the two parts:"
+ [ "(Int, String)"
+ , "(String, Int)"
+ , "(Bool, [Int])"
+ , ""
+ , "(Char, (Bool, Int))"
+ ]
+ , codeSlide "Accessing the parts"
+ "You can both pattern match, or use functions to access the \
+ \parts of a tuple:"
+ [ "isAdult (age, name) = age >= 18"
+ , ""
+ , "isAdult person = fst person >= 18"
+ ]
+ , codeSlide "Putting them together"
+ "You can put them together by using parenthesis and comma:"
+ [ "makePerson :: Int -> String -> (Int, String)"
+ , "makePerson age name = (age, name)"
+ ]
+ ]
View
52 seed/Chapter3/Step_3_2.hs
@@ -0,0 +1,52 @@
+module Step_3_2 where
+
+import Text.Html
+
+page = thehtml <<
+ [ header << (thetitle << "Output")
+ , body <<
+ [ h1 << "A to do list:"
+ , thediv << toDoHtml
+ ]
+ ]
+
+-- We've changed the type of toDoItems to include a Bool value to
+-- indicate if they've been done.
+
+toDoItems :: [(Bool, String)] -- a list of tuples: each a Bool and String
+toDoItems =
+ [ (True, "Pick up avacados")
+ , (True, "Make snacks")
+ , (False, "Clean house")
+ , (False, "Have party")
+ ]
+ -- Haskellers often format lists with long items in this funny way!
+
+renderToDo :: [String] -> [Html]
+renderToDo ts = map (li <<) ts
+
+toDoHtml :: Html
+toDoHtml = ulist << renderToDo toDoItems
+
+-- Try running this file. You'll find that the it doesn't compile, and gives
+-- an error on the line above. Just read the first two lines of the error and
+-- see if you can see what the compiler is trying to tell you about the problem
+-- with the types.
+
+-- Fix the renderToDo function to fix the problem.
+
+-- NEXT
+
+-- What did you end up doing with the Bool component of the tuple? Did you
+-- ignore it? Did you use it in the computation somehow? Did you notice that
+-- by having a clear type, you were forced to think about it?
+
+-- Change the renderToDo function to render the items that aren't done bold.
+
+-- NEXT
+
+-- For a challenge, change the renderToDo function to render only the first
+-- not done item bold.
+
+hint1 = map pred "Zpv!qspcbcmz!ibwf!up!hp!cbdl!up!opu!vtjoh!nbq/"
+hint2 = map pred "Zpv!qspcbcmz!xjmm!offe!bopuifs!ifmqfs!gvodujpo/"
View
53 seed/Chapter3/Step_3_3.hs
@@ -0,0 +1,53 @@
+module Step_3_3 where
+
+import Slides
+
+page = slideDeck "Chapter 3"
+ [ codeSlide "Bigger Tuples"
+ "Tuples can be larger: 3 or more elements. Here are some larger tuple \
+ \types:"
+ [ "(Int, String, Bool)"
+ , "(Int, Int, Int, Int) -- you can use the same type more than once"
+ , "(Bool, Bool, Bool, Bool,"
+ , " Bool, Bool, Bool, Bool) -- a byte? please, no!"
+ ]
+ , codeSlide "Bigger Tuples"
+ "There aren't any accessor functions defined for these, so you have \
+ \to pattern match to get their parts. Though you could define:"
+ [ "fst3 :: (a, b, c) -> a"
+ , "fst3 (a, b, c) = a"
+ , ""
+ , "snd3 :: (a, b, c) -> b"
+ , "snd3 (a, b, c) = b"
+ , ""
+ , "trd3 :: (a, b, c) -> c"
+ , "trd3 (a, b, c) = c"
+ ]
+ , pointSlide "Lists vs. Tuples"
+ "Lists and Tuples are different:"
+ [ "In a list the elements are all the same type."
+ , "In a tuple members can be different types."
+ , ""
+ , "A list can have any number of elements."
+ , "A tuple always has the number of members indicated by its type."
+ ]
+ , codeSlide "Type names"
+ "You can give a name to a type. This makes things more convient to \
+ \use:"
+ [ "type Person = (Int, String)"
+ , ""
+ , "isAdult :: Person -> Bool"
+ , "isAdult (age,name) = age >= 18"
+ ]
+ , codeSlide "Type names"
+ "Watch out: The type is just a synonym. amy and bob are still the \
+ \same type:"
+ [ "type Person = (Int, String)"
+ , ""
+ , "amy :: Person"
+ , "amy = (28, \"Amy\""
+ , ""
+ , "bob :: (Int, String)"
+ , "bob = (26, \"Bob\""
+ ]
+ ]
View
32 seed/Chapter3/Step_3_4.hs
@@ -0,0 +1,32 @@
+module Step_3_4 where
+
+import Text.Html
+
+page = thehtml <<
+ [ header << (thetitle << "Output")
+ , body <<
+ [ h1 << "A to do list:"
+ , thediv << toDoHtml
+ ]
+ ]
+
+-- Rewrite all of this to use a type synonym.
+
+toDoItems :: [(Bool, String)] -- a list of tuples: each a Bool and String
+toDoItems =
+ [ (True, "Pick up avacados")
+ , (True, "Make snacks")
+ , (False, "Clean house")
+ , (False, "Have party")
+ ]
+
+formatToDo :: (Bool, String) -> Html
+formatToDo (True,item) = li << item
+formatToDo (False,item) = li << bold << item
+
+renderToDo :: [(Bool, String)] -> [Html]
+renderToDo ts = map formatToDo ts
+
+toDoHtml :: Html
+toDoHtml = ulist << renderToDo toDoItems
+
View
5 seed/lib/Tutorial.hs
@@ -64,6 +64,11 @@ steps =
, Step 2 3 Code
, Step 2 4 Slides
, Step 2 5 Code
+ , Step 2 6 Code
+ , Step 3 1 Slides
+ , Step 3 2 Code
+ , Step 3 3 Slides
+ , Step 3 4 Code
]
stepUrls :: [String]

0 comments on commit 96feb24

Please sign in to comment.
Something went wrong with that request. Please try again.