COMP3400 Assignment 3 Written (Solution)

$ 29.99
Category:

Description

Paul Vrbik
In addition to this written work there are three coding questions. The written work is worth 25 points and the coding questions are worth 60 points totalling 85 points.
Either
The Functor and Applicative for the Either data-type is as follows:
instance Functor (Either a) where fmap f (Right x) = Right (f x) fmap f (Left x) = Left x
instance Applicative (Either e) where pure = Right
Left e <*> _ = Left e
Right f <*> r = fmap f r
Question 1. [4marks]
Show Either satisfies the second functor law:
fmap (g . h) = fmap g . fmap h
Question 2. [6marks]
Show Either satisfies the third applicative law:
x <*> pure y = pure (g -> g y) <*> x
1
ZipWith
Recall the alternate definition for a list applicative given in tutorial 8.
1 instance Functor [] where
2 fmap _ [] = []
3 fmap g (x:xs) = g x : (fmap g xs)
4
5 instance Applicative [] where
6 pure f = repeat f
7 [] <*> _ = []
8 _ <*> [] = []
9 (f:fs) <*> (x:xs) = (f x) : (fs <*> xs)
When writing your proofs use the line numbers given above when justifying your steps.
Question 3. [7marks]
Show your Applicative satisfies the second applicative law:
pure (g x) = pure g <*> pure x
Question 4. [8marks]
Show your Applicative satisfies the forth applicative law:
x <*> (y <*> z) = (pure (.) <*> x <*> y) <*> z
2

Reviews

There are no reviews yet.

Be the first to review “COMP3400 Assignment 3 Written (Solution)”

Your email address will not be published. Required fields are marked *