小编V98*_*801的帖子

Haskell找到完美的正方形 - 获取类型错误

我刚开始学习haskell,我试图实现一个简单的函数来检查数字是否是平方根.我认为我在理解Haskell类型系统时遇到了一些问题 - 我唯一的其他编程经验是ruby和一些Java.这是我到目前为止所做的(对不起,如果它真的很愚蠢):

isPerfectSquare :: (RealFloat t) => t -> Bool
isPerfectSquare n = 
    (sqrt n) == (truncate (sqrt n))
Run Code Online (Sandbox Code Playgroud)

这就是我在红宝石中所做的......但在这里它给了我这个错误:

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 `(==)', …
Run Code Online (Sandbox Code Playgroud)

haskell types functional-programming casting

2
推荐指数
1
解决办法
323
查看次数

标签 统计

casting ×1

functional-programming ×1

haskell ×1

types ×1