我试图定义:
square :: Integer -> Integer
square = round . (** 2)
我得到:
<interactive>:9:9: error:
• No instance for (RealFrac Integer) arising from a use of ‘round’
• In the first argument of ‘(.)’, namely ‘round’
In the expression: round . (** 2)
In an equation for ‘square’: square = round . (** 2)
<interactive>:9:18: error:
• No instance for (Floating Integer)
arising from an operator section
• In the second argument of ‘(.)’, namely ‘(** 2)’
In the expression: round . (** 2)
In an equation for ‘square’: square = round . (** 2)
我对这种语言还是新手,我似乎无法将 Floating 实例转换为 Integer。有人知道我该怎么做吗?
Haskell 有多个不同类型的指数函数:
- (^) :: (Num a, Integral b) => a -> b -> a http://hackage.haskell.org/package/base-4.9.0.0/docs/Prelude.html#v:-94-
- (^^) :: (Fractional a, Integral b) => a -> b -> a http://hackage.haskell.org/package/base-4.9.0.0/docs/Prelude.html#v:-94--94-
- (**) :: Floating a => a -> a -> a http://hackage.haskell.org/package/base-4.9.0.0/docs/Prelude.html#v:-42--42-
您正在寻找的只是(^)
。有了它,你甚至不需要round
:
square :: Integer -> Integer
square = (^ 2)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)