module Ex where import Data.Int (fromString) import Data.Maybe (Maybe(..)) import Data.Tuple (Tuple(..)) import Prelude import Unsafe.Coerce (unsafeCoerce) -- -- Multiple choice -- -- Exercise 2 f :: Char -> String f = unsafeCoerce unit g :: String -> Array String g = unsafeCoerce unit e :: Char -> Array String e = g <<< f -- Exercise 5 f' :: forall a. a -> a f' x = x g' :: Boolean g' = f' true -- -- Let's write code -- -- Exercise 1 tensDigit :: Int -> Int tensDigit x = d where xLast = x `div` 10 d = xLast `mod` 10 -- Exercise 2 hunsD :: Int -> Int hunsD x = d where xLast = x `div` 100 d = xLast `mod` 10 foldBool :: forall a. a -> a -> Boolean -> a foldBool x y z = case z of false -> x true -> y findBool2 :: forall a. a -> a -> Boolean -> a findBool2 x y z | z = y | otherwise = x foldBool3 :: forall a. a -> a -> Boolean -> a foldBool3 x _ false = x foldBool3 _ y true = y -- Exercise 3 g'' :: forall a b c. (a -> b) -> Tuple a c -> Tuple b c g'' f'' (Tuple a c) = Tuple (f'' a) c -- Exercise 5 -- id :: a -> a -- id = x = x roundTrip :: Int -> Int roundTrip = fromMaybe <<< fromString <<< show where fromMaybe (Just n) = n fromMaybe Nothing = unsafeCoerce unit