2006-09-18久々のHaskell
■ 基本的な値 - タプル
複数の値を纏めて扱うタプル。タプルいいよな。でもタプルが組み込まれている言語ってPythonくらいしか知らないけど。
ということで、同値性チェック。
a = (3::Int,"hoge")
b = (3, "hoge")
c = (3::Integer, "hoge")
main = do print $ a == b
print $ b == c
print $ c == a
TeqT.hs:6:25:
Couldn't match `Int' against `Integer'
Expected type: (Int, [Char])
Inferred type: (Integer, [Char])
In the second argument of `(==)', namely `c'
In the second argument of `($)', namely `b == c'
あれ?「b == c」でダメか。「c == a」でダメって怒られると予想したんだけど…。タプルに数値リテラルを適用すると、Intになっちゃうってことかな。それとも、先に「a == b」の評価をしたが故にfst bがIntと思われちゃったのかな。
ということで、「a == b」と「b == c」の実行順序を入れ替えてみる。
a = (3::Int,"hoge")
b = (3, "hoge")
c = (3::Integer, "hoge")
main = do print $ b == c
print $ a == b
print $ c == a
TeqT.hs:6:25:
Couldn't match `Int' against `Integer'
Expected type: (Int, [Char])
Inferred type: (Integer, [Char])
In the second argument of `(==)', namely `b'
In the second argument of `($)', namely `a == b'
やっぱり「a == b」で型が違うと怒られる。だから、bの型はIntegerなんだろう。数値リテラルみたいに文脈で型が決まるものは、そうは言えども一度決まっちゃったらずっとその型として扱われるのか。面白いー。
Antione2007/07/02 05:07http://64fdebd2c057be50b9c19d9a1e08d879-t.xxpogy.org <a href="http://64fdebd2c057be50b9c19d9a1e08d879-h.xxpogy.org">64fdebd2c057be50b9c19d9a1e08d879</a> [url]http://64fdebd2c057be50b9c19d9a1e08d879-b1.xxpogy.org[/url] [url=http://64fdebd2c057be50b9c19d9a1e08d879-b2.xxpogy.org]64fdebd2c057be50b9c19d9a1e08d879[/url] [u]http://64fdebd2c057be50b9c19d9a1e08d879-b3.xxpogy.org[/u] 9dc75301cfe28cfb0fb3d1863c1dee5e