## 2017年2月12日日曜日

### Haskell - 単純なデータ型 - 組(タプル)

コード(Emacs)

```-- 2.4.1
-- cross (f, g) . cross (h, k) undefined
-- = (pair (f . fst, g . snd)) . (pair (h . fst, k . snd)) undefined
-- = (pair (f . fst, g . snd)) (h . fst undefined, k . snd undefined)
-- = (f . h . fst undefined, g . k . snd undefined)
-- = pair (f . h . fst, g . k . snd) undefined
-- = cross (f . h, g . k) undefined

-- 2.4.2
data Triple a b c = Triple a b c

-- 2.4.3
age :: (Integer, Integer, Integer) -> (Integer, Integer, Integer) -> Integer
age (d1, m1, y1) (d2, m2, y2)
| m1 > m2 = a
| m1 < m2 = a - 1
| d1 >= d2 = a
| otherwise = a - 1
where a = y1 - y2

-- 2.4.4
instance (Enum a, Enum b) => Enum (a, b) where
toEnum n = toEnum n
```

```\$ ghci sample4.hs
GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Main             ( sample4.hs, interpreted )
*Main> :t Triple 1 2 3
Triple 1 2 3 :: (Num c, Num b, Num a) => Triple a b c
*Main> :t Triple 1 'a' undefined
Triple 1 'a' undefined :: Num a => Triple a Char c
*Main> age (9, 1, 2010) (10, 1, 2000)
9
*Main> age (10, 1, 2010) (10, 1, 2000)
10
*Main> age (11, 1, 2010) (10, 1, 2000)
10
*Main> age (10, 1, 2010) (10, 2, 2000)
9
*Main> age (10, 2, 2010) (10, 2, 2000)
10
*Main> age (10, 3, 2010) (10, 2, 2000)
10
*Main> :q
Leaving GHCi.
\$
```