Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 45 lines (32 sloc) 1.213 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 41 42 43 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)


Something went wrong with that request. Please try again.