2
我刚开始学习haskell,并试图实现一个简单的函数来检查数字是否是平方根。我想我有一些理解Haskell类型系统的问题 - 我唯一的其他编程经验是ruby和一些Java。这是我迄今(很抱歉,如果它真的傻):Haskell找到完美的方块 - 获取类型错误
isPerfectSquare :: (RealFloat t) => t -> Bool
isPerfectSquare n =
(sqrt n) == (truncate (sqrt n))
这就是我会做的红宝石......但在这里它给了我这个错误:
Could not deduce (Integral t) arising from a use of `truncate'
from the context (RealFloat t)
bound by the type signature for
isPerfectSquare :: RealFloat t => t -> Bool
at more.hs:(73,1)-(74,35)
Possible fix:
add (Integral t) to the context of
the type signature for isPerfectSquare :: RealFloat t => t -> Bool
In the second argument of `(==)', namely `(truncate (sqrt n))'
In the expression: (sqrt n) == (truncate (sqrt n))
In an equation for `isPerfectSquare':
isPerfectSquare n = (sqrt n) == (truncate (sqrt n))
Failed, modules loaded: none.
莫非你请解释什么是问题,如何解决问题,最好是我不了解的基本概念?提前致谢。
感谢,这正是我一直在寻找!我想我只需要学习很多东西。 – V9801 2011-12-23 02:19:04