Question 1: Types and Functions

with value [1, 2, 3, …] which is infinitely long. (i) Write a definition of posInts.

Answer:

posInts :: [Int]
posInts =
 let f x = x : f (x+1)
 in f 1

Question 2: Embedded Domain Specific Languages

Question 3: Monads and Equational Reasoning

Question 4: Interactive Programming

Maybe a, and safeTail :: [a] Maybe [a].

Answer:

safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:xs) = Just x
 
safeTail :: [a] -> Maybe [a]
safeTail [] = Nothing
safeTail (x:xs) = Just xs