IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

ラムダ式を使うときと使わないときの型推論の差

よくわかっていない。。。

ラムダ式の場合は型がInteger
Prelude> let a = \x -> x * 2
Prelude> :t a
a :: Integer -> Integer
Prelude> a 5
10
Prelude> a 1.2

<interactive>:4:3:
    No instance for (Fractional Integer) arising from the literal `1.2'
    Possible fix: add an instance declaration for (Fractional Integer)
    In the first argument of `a', namely `1.2'
    In the expression: a 1.2
    In an equation for `it': it = a 1.2
通常(?)の関数定義の場合は型がNum a
Prelude> let a' x = x * 2
Prelude> :t a'
a' :: Num a => a -> a
Prelude> a' 5
10
Prelude> a' 1.2
2.4
Prelude> 
ポイントフリースタイルの場合は型がInteger
Prelude> let a'' = (*2)
Prelude> :t a''
a'' :: Integer -> Integer
Prelude> a'' 2
4
Prelude> a'' 2.3

<interactive>:15:5:
    No instance for (Fractional Integer) arising from the literal `2.3'
    Possible fix: add an instance declaration for (Fractional Integer)
    In the first argument of a'', namely `2.3'
    In the expression: a'' 2.3
    In an equation for `it': it = a'' 2.3
Prelude>