DAY 18
0
Software Development

## mostly:functional 第十七章：當我們談論容器時，我們在談論什麼？

-- E. E. Cummings, i carry your heart with me(i carry it in

-- 0430

## 串列與它的型別

``````-- Haskell 語法
data [] a = [] | a : [a]
``````

``````-- Haskell 語法
list = [1, 2, 3, 4, 5] -- 其實這是一種語法糖。真正的樣子，是下面這樣：

list' = 1 : (2 : (3: (4: (5: []))))
``````

## 元組與它的型別

``````-- Haskell 語法
data (,) a b = (,) a b

data (,,) a b c = (,,) a b c -- 三元組
``````

``````-- Haskell 語法
type PuahPue = (Bool, Bool)

siann_pue = (True, False)
siau_pue = (True, True)
``````

``````-- Haskell 語法
data Pip = Two | Three | Four | Five | Six | Seven | Eight | Nine |
Ten | Jack | Queen | King | Ace
data Suit = Spades | Hearts | Diamonds | Clubs

type Card = (Pip, Suit) -- 13 * 4 種可能

card1 = (Ace, Spades)
``````

## Maybe

`Maybe` 的型別定義是這樣：

``````--- Haskell 語法
data Maybe a = Nothing | Just a
``````

``````num1 = Just 1
var2 = Nothing
num3 = Nothing :: Maybe Integer
``````

## Either

``````-- Haskell 語法
data Either a b = Left a | Right b
``````

``````-- Haskell 語法
get_user_id = Right 100

get_user_id_failed = Left "Network error"
``````

## 為什麼要說這些東西是容器？

<故事待補>

*[to be continue]