Write a Traversable instance for the datatype provided, filling in any required superclasses. Use QuickCheck to validate your instances.
Identity
Write a Traversable instance for Identity.
newtype Identity a = Identity a deriving (Eq, Ord, Show)
instance Traversable Identity where
traverse = undefinedSolution file (can be run as a script)
Constant
newtype Constant a b = Constant {getConstant :: a}Solution file (can be run as a script)
Maybe
data Optional a = Nada | Yep aSolution file (can be run as a script)
List
data List a = Nil | Cons a (List a)Solution file (can be run as a script)
Three
data Three a b c = Three a b cSolution file (can be run as a script)
Pair
data Pair a b = Pair a bSolution file (can be run as a script)
Big
When you have more than one value of type b, you'll want to use Monoid and Applicative for the Foldable and Traversable instances respectively.
data Big a b = Big a b bSolution file (can be run as a script)
Bigger
data Bigger a b = Bigger a b b bSolution file (can be run as a script)
S
{-# LANGUAGE FlexibleContexts #-}
module SkiFree where
import Test.QuickCheck
import Test.QuickCheck.Checkers
data S n a = S (n a) a deriving (Eq, Show)
instance (Functor n
, Arbitrary (n a)
, Arbitrary a )
=> Arbitrary (S n a) where
arbitrary = S <$> arbitrary <*> arbitrary
instance (Applicative n
, Testable (n Property)
, EqProp a)
=> EqProp (S n a) where
(S x y) =-= (S p q) =
(property $ (=-=) <$> x <*> p)
.&. (y =-= q)
instance Traversable n => Traversable (S n) where
traverse = undefined
main = sampl' (arbitrary :: Gen (S [] Int))Solution file (can be run as a standalone script)
Write the following instances for Tree.
data Tree a =
Empty
| Leaf a
| Node (Tree a) a (Tree a)
deriving (Eq, Show)
instance Functor Tree where
fmap = undefined
-- foldMap is a bit easier
-- and looks more natural,
-- but you can do foldr too
-- for extra credit.
instance Foldable Tree where
foldMap = undefined
instance Traversable Tree where
traverse = undefinedHints:
- For
foldMap, thinkFunctorbut with someMonoidthrown in. - For
traverse, thinkFunctorbut with someFunctorthrown in. Solution file (can be run as a standalone script)