Permalink
Browse files

reordered sections and inserted more about Maybe

  • Loading branch information...
1 parent 9a1da4f commit 359b940d1994230ede9b88fe0b5a4cdb61a0553b @mzero committed Oct 12, 2011
Showing with 260 additions and 161 deletions.
  1. +13 −21 Part2a.hs
  2. +2 −10 Part2b.hs
  3. +2 −14 Part2c.hs
  4. +0 −1 Part2d.hs
  5. +38 −0 Part3a.hs
  6. +1 −1 Part3.hs → Part4.hs
  7. +62 −33 slides.html
  8. +142 −81 slides.md
View
@@ -1,30 +1,22 @@
module Part2a where
-data List α = Nil
- | Cons α (List α)
+data List α = EndOfList
+ | Link α (List α)
deriving Show -- makes printing out results possible
-empty = Nil
-oneWord = Cons "apple" Nil
-twoWords = Cons "banana" (Cons "cantaloupe" Nil)
+empty = EndOfList
+oneWord = Link "apple" EndOfList
+twoWords = Link "banana" (Link "cantaloupe" EndOfList)
-mystery1 = Cons "pear" empty
-mystery2 = Cons "peach" oneWord
-mystery3 = Cons "pineapple" mystery3
--- mystery4 = Cons 42 (Cons "apple" Nil) -- won't compile
+mystery1 = Link "pear" empty
+mystery2 = Link "peach" oneWord
+mystery3 = Link "pineapple" mystery3
+-- mystery4 = Link 42 (Link "apple" EndOfList) -- won't compile
dropOne :: List a -> List a
-dropOne (Cons first rest) = rest
-dropOne Nil = Nil
+dropOne (Link first rest) = rest
+dropOne EndOfList = EndOfList
justOne :: List a -> List a
-justOne (Cons a _) = Cons a Nil
-justOne Nil = Nil
-
-firstOne :: List a -> a
-firstOne (Cons a _) = a
-firstOne Nil = error "O Noes!"
-
-maybeFirstOne :: a -> List a -> a
-maybeFirstOne def (Cons first rest) = first
-maybeFirstOne def Nil = def
+justOne (Link a _) = Link a EndOfList
+justOne EndOfList = EndOfList
View
@@ -1,7 +1,7 @@
module Part2b where
--- data [a] = [] | a : [a] -- already built in
--- infixr 5 : -- already built in
+-- data [] a = [] | a : [a] -- already in standard library
+-- infixr 5 : -- already in standard library
empty = []
oneWord = "apple" : []
@@ -19,11 +19,3 @@ dropOne [] = []
justOne :: [a] -> [a]
justOne (a:_) = a:[]
justOne [] = []
-
-firstOne :: [a] -> a
-firstOne (a:_) = a
-firstOne [] = error "O Noes!"
-
-maybeFirstOne :: a -> [a] -> a
-maybeFirstOne def (first:rest) = first
-maybeFirstOne def [] = def
View
@@ -1,7 +1,7 @@
module Part2c where
--- data [a] = [] | a : [a] -- already built in
--- infixr 5 : -- already built in
+-- data [] a = [] | a : [a] -- already in standard library
+-- infixr 5 : -- already in standard library
empty = []
oneWord = ["apple"] -- syntatic sugar
@@ -19,15 +19,3 @@ dropOne [] = []
justOne :: [a] -> [a] -- don't confuse these "[a]"s
justOne (a:_) = [a] -- with this "[a]"
justOne [] = []
-
-firstOne :: [a] -> a -- normally called 'head'
-firstOne (a:_) = a
-firstOne [] = error "O Noes!"
-
-maybeFirstOne :: a -> [a] -> a
-maybeFirstOne def (first:rest) = first
-maybeFirstOne def [] = def
-
-firstOne' :: [a] -> Maybe a
-firstOne' (a:_) = Just a
-firstOne' [] = Nothing
View
@@ -1,6 +1,5 @@
module Part2d where
-
findAfterStar :: String -> Maybe Char
findAfterStar (c:d:r) =
if c == '*' then Just d
View
@@ -0,0 +1,38 @@
+module Part3a where
+
+import Data.Time.Calendar
+
+
+-- data Maybe a = Nothing | Just a -- part of the standard library
+
+
+firstOne :: [a] -> a -- normally called 'head'
+firstOne (a:_) = a
+firstOne [] = error "O Noes!"
+
+firstOne' :: [a] -> Maybe a
+firstOne' (a:_) = Just a
+firstOne' [] = Nothing
+
+
+addAWeek :: Day -> Day
+addAWeek d = addDays 7 d
+
+interestingDates :: [Day]
+interestingDates =
+ [ fromGregorian 1966 9 8 -- first episode of Star Trek airs
+ , fromGregorian 1969 6 21 -- first person on the moon
+ , fromGregorian 1969 10 29 -- first ARPANET message sent
+ ]
+
+anInterestingDate :: Maybe Day
+anInterestingDate = firstOne' interestingDates
+
+aWeekLater :: Maybe Day
+aWeekLater = fmap addAWeek anInterestingDate
+
+maybeAddAWeek :: Maybe Day -> Maybe Day
+maybeAddAWeek = fmap addAWeek
+
+aWeekLater' :: Maybe Day
+aWeekLater' = maybeAddAWeek anInterestingDate
View
@@ -1,4 +1,4 @@
-module Part3 where
+module Part4 where
runLengthEncode :: Eq a => [a] -> [(a, Int)]
runLengthEncode [] = []
View

Large diffs are not rendered by default.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 359b940

Please sign in to comment.