相关疑难解决方法(0)

Haskell:默认约束类型

考虑这个例子:

applyKTimes :: Integral i => i -> (a -> a) -> a -> a
applyKTimes 0 _ x = x
applyKTimes k f x = applyKTimes (k-1) f (f x)

applyThrice :: (a -> a) -> a -> a
applyThrice = applyKTimes 3
Run Code Online (Sandbox Code Playgroud)

该3在applyThrice通过GHC默认为一个Integer与编译时如图所示-Wall:

Warning: Defaulting the following constraint(s) to type 'Integer'
         'Integral t'
           arising from a use of 'applyKTimes'
Run Code Online (Sandbox Code Playgroud)

所以我猜这Integer是默认的Integral a => a.

  • 有没有办法为其他约束定义"默认类型"?
  • 使用默认类型不好的做法?(使用时确实抱怨-Wall …

haskell typeclass

18
推荐指数
1
解决办法
2980
查看次数

标签 统计

haskell ×1

typeclass ×1