Module 03: Polymorphism and Lists
- Record your team members here:
In this module we will complete our initial exploration of the Haskell programming language. For today, choose whoever you wish to start as the driver.
Maybe
Note that the above definition of Maybe does not have bird tracks in front of it since it is already defined in the standard library; I just wanted to show you its definition.
w :: Maybe Int
w = Just 3
x :: Maybe String
x = Just "hello"
y :: Maybe Char
y = Nothing
z :: Maybe (Maybe Int)
z = Just Nothing
safeDiv :: Integer -> Integer -> Maybe Integer
safeDiv _ 0 = Nothing
safeDiv x y = Just (x `div` y)
showDiv :: Integer -> Integer -> String
showDiv x y = case safeDiv x y of
Nothing -> "Division by zero"
Just z -> "Result is " ++ show zGive two example values of type
Maybe Bool.How many distinct values of type
Maybe (Maybe Bool)are there? List all of them.How is
safeDivdifferent fromdiv?Try
showDivon some examples. Describe in words what it does.What does the
ain the definition ofMayberepresent?What Java feature does the
aremind you of?Write a function
plusMaybe :: Maybe Integer -> Maybe Integer -> Maybe Integerwhich performs addition if both arguments areJust, and returnsNothingotherwise.
(You know the drill) The reporter should be prepared to share your version of plusMaybe with another group.
Lists
ROTATE ROLES
ints :: [Integer]
ints = [3, 5, 92]
noInts :: [Integer]
noInts = []
moreInts :: [Integer]
moreInts = 7 : ints
yetMoreInts :: [Integer]
yetMoreInts = 4 : 2 : ints
someInts :: [Integer]
someInts = 8 : 42 : noInts
ints2 :: [Integer]
ints2 = 3 : 5 : 92 : []Evaluate
length intsandlength noInts.Explain what
[]means.Evaluate
moreIntsandlength moreInts.Do the same for
yetMoreInts.Now evaluate
ints. Has it changed?Write an expression
esuch thatlength eevaluates to6.Explain what the
(:)operator does.What will
someIntsevaluate to? How aboutlength someInts? Write down your guesses before typing them into GHCi.Now check your guesses.
Evaluate
ints2. What do you notice?

Strings
ROTATE ROLES
greeting :: String
greeting = "Hello"
greeting2 :: String
greeting2 = ['H','e','l','l','o']
greeting3 :: [Char]
greeting3 = ['H','e','l','l','o']
everyone :: String
everyone = "world"Evaluate
greeting,greeting2, andgreeting3. What differences do you notice? What can you conclude? (Hint: try typing:info Stringat the GHCi prompt.)Try evaluating
greeting : everyone. What happens?Now try evaluating
greeting ++ everyone. What happens?Explain the difference between
(:)and(++).What are the types of
(:)and(++)? Do they match your explanation above?Explain the difference between
'a'and"a".Write an expression using
greetingandeveryonewhich evaluates to"Hello, world!".

List pattern matching
ROTATE ROLES
listLength [] = (0 :: Integer)
listLength (_:xs) = 1 + listLength xs
startsWith :: String -> String -> Bool
startsWith [] _ = undefined
startsWith (_:_) [] = undefined
startsWith (x:xs) (y:ys) = undefinedWhat is the type of
listLength? (Feel free to ask GHCi.)The type of
listLengthprobably has a lowercase letter in it, liketora. Explain what the type oflistLengthmeans.Evaluate
startsWith "cat" "catastrophe". What happens?Complete the definition of
startsWithby replacingundefinedwith appropriate expressions.startsWithshould test whether the second argument has the first argument as a prefix. For example:startsWith "cat" "catastrophe" -> True startsWith "car" "catastrophe" -> False startsWith "ban" "banana" -> True startsWith "" "dog" -> True startsWith "at" "catastrophe" -> False startsWith "dog" "" -> FalseWrite a function
contains :: String -> String -> Bool, which tests whether the second argument contains the first argument as a (contiguous) substring. For example,contains "cat" "catastrophe" -> True contains "cat" "concatenate" -> True contains "cat" "create" -> False contains "fly" "old lady" -> FalseHint: use
startsWith.Write a function
listReverse :: [a] -> [a]which reverses a list. For example,listReverse [] -> [] listReverse [1,2,3] -> [3,2,1] listReverse "Hello" -> "olleH"DO NOT look at any existing implementation of reverse.