Haskell调整了函数类型

时间:2015-09-25 08:19:54

标签: haskell currying

我对curryuncurry函数返回的类型感到有些困惑:

Prelude> let pairMax (a,b) = if a > b then a else b
Prelude> :t pairMax
pairMax :: Ord a => (a, a) -> a
Prelude> let pairMaxCurried = curry pairMax
Prelude> :t pairMaxCurried 
pairMaxCurried :: () -> () -> ()

我不明白为什么curry函数的类型只包含Unit类型。试图应用pairMaxCurried毫不奇怪会导致错误:

Prelude> pairMaxCurried 1 2

<interactive>:10:16:
    No instance for (Num ()) arising from the literal `1'
    Possible fix: add an instance declaration for (Num ())
    In the first argument of `pairMaxCurried', namely `1'
    In the expression: pairMaxCurried 1 2
    In an equation for `it': it = pairMaxCurried 1 2

虽然在同一条线上进行cur and和申请很好:

Prelude> (curry pairMax) 1 2
2

同样适用于uncurry

Prelude> let twoMax a b = if a > b then a else b
Prelude> :t twoMax
twoMax :: Ord a => a -> a -> a
Prelude> let twoMaxUncurried = uncurry twoMax
Prelude> :t twoMaxUncurried
twoMaxUncurried :: ((), ()) -> ()

声明显式类型没有帮助:

Prelude> let pairMaxCurried = (curry pairMax) :: Ord a => a -> a -> a
Prelude> :t pairMaxCurried 
pairMaxCurried :: () -> () -> ()

问题1。为什么未正确推断出类型?

问题2。有没有办法将curried / uncurried函数绑定到名称,同时保留正确的类型?

0 个答案:

没有答案