Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell中地板和sqrt的联合使用_Haskell_Sqrt_Floor - Fatal编程技术网

Haskell中地板和sqrt的联合使用

Haskell中地板和sqrt的联合使用,haskell,sqrt,floor,Haskell,Sqrt,Floor,我希望我的函数读入一个整数,并返回向下舍入到最接近整数的平方根。这就是我尝试过的: roundSqrt :: Int -> Int roundSqrt x = floor (sqrt x) 我得到的错误是,“无法推断(浮动a)是由使用-sqrt引起的”,但我不明白这意味着什么。sqrt的类型是: λ> :t sqrt sqrt :: Floating a => a -> a 地板类型为: λ> ::t floor floor :: (RealFrac a,

我希望我的函数读入一个整数,并返回向下舍入到最接近整数的平方根。这就是我尝试过的:

roundSqrt :: Int -> Int
roundSqrt x = floor (sqrt x)  
我得到的错误是,“无法推断(浮动a)是由使用-sqrt引起的”,但我不明白这意味着什么。

sqrt的类型是:

λ> :t sqrt
sqrt :: Floating a => a -> a
地板类型为:

λ> ::t floor
floor :: (RealFrac a, Integral b) => a -> b
因此,
sqrt
需要一个具有
Floating
约束的类型。您可以使用
from integral
功能来实现:

roundSqrt :: Int -> Int
roundSqrt x = floor (sqrt (fromIntegral x))  

然后,通过使用组合操作符:
roundSqrt=floor来摆脱样板文件。sqrt。fromIntegral