Created
November 27, 2019 08:23
-
-
Save chansey97/62e45ad98fa413291986d9978ea642d1 to your computer and use it in GitHub Desktop.
ctfp-14.3.6-correct-solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DeriveFunctor, TypeFamilies #-} | |
module Main where | |
data Pair a = Pair a a | |
deriving (Eq) | |
getFst :: Pair a -> a | |
getFst (Pair x1 _) = x1 | |
getSnd :: Pair a -> a | |
getSnd (Pair _ x2) = x2 | |
class Representable f where | |
type Rep f :: * | |
tabulate :: (Rep f -> x) -> f x | |
index :: f x -> Rep f -> x | |
instance Representable Pair where | |
type Rep Pair = Bool | |
tabulate f = Pair (f True) (f False) -- fmap f Pair(Bool, False) | |
index (Pair x1 x2) = \bool -> if bool==True then x1 else x2 | |
-- index (tabulate f) | |
f :: Bool -> String | |
f True = "A" | |
f False = "B" | |
tabulatef :: Pair String | |
tabulatef = tabulate f | |
indextabulatef :: Bool -> String | |
indextabulatef = index tabulatef | |
check1a = indextabulatef True == "A" | |
check1b = indextabulatef False == "B" | |
-- tabulate (index Pair x1 x2) | |
pair :: Pair String | |
pair = Pair "A" "B" | |
indexpair :: Bool->String | |
indexpair = index pair | |
tabulateindexpair :: Pair String | |
tabulateindexpair = tabulate indexpair | |
check2 = tabulateindexpair == Pair "A" "B" | |
main = do | |
putStrLn "test" | |
putStrLn (show check1a) | |
putStrLn (show check1b) | |
putStrLn (show check2) | |
putStrLn (show (getFst tabulateindexpair)) | |
putStrLn (show (getSnd tabulateindexpair)) | |
--GHCi, version 8.6.5 | |
--test | |
--True | |
--True | |
--True | |
--"A" | |
--"B" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment