Yet Another Haskell 3章 (2)
3.2 Pairs, Triples and More
タプル(tuple)に関して
Haskellでは、タプルが使える。
タプルのそれぞれの要素は同じ型でなくても良い。
ex. (5, "hello"), (1, 2, 3, 4)
関数fstとsndが定義されていて、これを使って、pair(2-tuple)の要素を取り出すことができる。
Prelude> fst (5, "hello")
5
Prelude> snd (5, "hello")
"hello"
fstとsndはpairのみにしか使えない。
Prelude> fst (1,2,3)
<interactive>:8:5:
Couldn't match expected type `(a0, b0)'
with actual type `(t0, t1, t2)'
In the first argument of `fst', namely `(1, 2, 3)'
In the expression: fst (1, 2, 3)
In an equation for `it': it = fst (1, 2, 3)
Exercise 3.2
Prelude> snd (fst ( (1, 'a'), "foo"))
'a'