diff --git a/Exercises/exercise-5/Solution.hs b/Exercises/exercise-5/Solution.hs index dbbd88b..40c0bca 100644 --- a/Exercises/exercise-5/Solution.hs +++ b/Exercises/exercise-5/Solution.hs @@ -2,10 +2,11 @@ import Data.Bits import Data.Char import Data.Functor.Contravariant -data Result t = +data Result i o = Result { - actual :: t, - expected :: t, + input :: i, + actual :: o, + expected :: o, result :: Bool } deriving (Show, Eq, Ord) @@ -17,28 +18,30 @@ newtype Boxed a = Boxed {unbox :: a} instance Functor Boxed where fmap f (Boxed a) = Boxed (f a) -test11 = Result actual expected (actual == expected) +test11 = Result x actual expected (actual == expected) where + x = 1 actual = unbox (fmap id b) expected = unbox b - b = Boxed { unbox = 1 :: Integer } + b = Boxed { unbox = x :: Integer } -- >>> test11 --- Result {actual = 1, expected = 1, result = True} +-- Result {input = 1, actual = 1, expected = 1, result = True} -- -test12 = Result actual expected (actual == expected) +test12 = Result x actual expected (actual == expected) where + x = 1 actual = unbox (fmap (f . g) b) expected = unbox (fmap f $ fmap g b) - b = Boxed { unbox = 1 :: Integer } + b = Boxed { unbox = x :: Integer } f = (+9) g = (*11) -- >>> test12 --- Result {actual = 20, expected = 20, result = True} +-- Result {input = 1, actual = 20, expected = 20, result = True} -- -------------------- @@ -49,30 +52,30 @@ newtype FromInt b = FromInt {fun :: Int -> b} instance Functor FromInt where fmap f (FromInt b) = FromInt (\ x -> f (b x)) -test21 = Result actual expected (actual == expected) +test21 = Result x actual expected (actual == expected) where + x = 2 actual = ((fun (fmap id converter)) x) expected = (fun (converter) x) converter = FromInt {fun = \ val -> val * (-1)} - x = 2 -- >>> test21 --- Result {actual = -2, expected = -2, result = True} +-- Result {input = 2, actual = -2, expected = -2, result = True} -- -test22 = Result actual expected (actual == expected) +test22 = Result x actual expected (actual == expected) where + x = 7 actual = (fun (fmap g $ fmap f converter)) x expected = (fun ((g . f) <$> converter)) x converter = FromInt {fun = \ val -> chr ((ord 'A') + val)} f = \ char -> char:"B" g = \ str -> str ++ "C" - x = 7 -- >>> test22 --- Result {actual = "HBC", expected = "HBC", result = True} +-- Result {input = 7, actual = "HBC", expected = "HBC", result = True} -- -------------------- @@ -90,8 +93,8 @@ strHash value = realHash 0 value realHash acc (char:str) = realHash (acc + ((ord char) .&. 0b11010110)) str test3 = [ - (Result strExpected strActual (strExpected == strActual)), - (Result expected actual (expected == actual)) + (Result myStr strExpected strActual (strExpected == strActual)), + (Result [myChar] expected actual (expected == actual)) ] where -- Create object `toInt` for converting strings to an Int hash (using the "strHash" function) @@ -119,6 +122,6 @@ test3 = [ -- Showing the result of the `String` and the `Char` test -- >>> mapM_ putStrLn (map show test3) --- Result {actual = 712, expected = 712, result = True} --- Result {actual = 86, expected = 86, result = True} +-- Result {input = "Hello World", actual = 712, expected = 712, result = True} +-- Result {input = "V", actual = 86, expected = 86, result = True} --