summaryrefslogtreecommitdiff
path: root/Haskell-book/07/src/Ex.purs
blob: 340917ea819e3e79b84da9bb86d3541b5ab4c20d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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