Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.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
List Haskell列表理解(打印列表元素的sqrt)_List_Haskell_List Comprehension - Fatal编程技术网

List Haskell列表理解(打印列表元素的sqrt)

List Haskell列表理解(打印列表元素的sqrt),list,haskell,list-comprehension,List,Haskell,List Comprehension,我有GHCi,版本7.8.3。我想计算sqrt项的总和,它可以被10整除 如果我写[x | x问题来自这样一个事实:当您使用mod时,数字的类型必须是Integral a=>a,而当您使用sqrt时,数字的类型必须是Floating a=>a。GHC知道没有适合这两个约束的类型,尽管因为您正在执行它在GHCi中,无论出于何种原因,错误消息基本上都是无用的。错误消息是这样的,因为GHCi使用打印,它调用显示,出于某种原因,这是第一个被检查的约束。由于没有具有约束的类型显示,积分,和浮动,它不进行打

我有GHCi,版本7.8.3。我想计算sqrt项的总和,它可以被10整除


如果我写
[x | x问题来自这样一个事实:当您使用
mod
时,数字的类型必须是
Integral a=>a
,而当您使用
sqrt
时,数字的类型必须是
Floating a=>a
。GHC知道没有适合这两个约束的类型,尽管因为您正在执行它在GHCi中,无论出于何种原因,错误消息基本上都是无用的。错误消息是这样的,因为GHCi使用
打印
,它调用
显示
,出于某种原因,这是第一个被检查的约束。由于没有具有约束的类型
显示
积分
,和
浮动
,它不进行打字检查

您的其他两个示例进行了类型检查,因为它们只使用了
mod
sqrt
中的一个。在应用
sqrt
之前,您可以使用
from integral
将这两个示例组合起来:

sum [sqrt $ fromIntegral x | x <- [10..100], x `mod` 10 == 0]

sum[sqrt$from Integral x | x使我想知道是否有任何类型的
Integral
Float
都具有合理的实例。我注意到类似
fx=sqrt(mod x10)
的类型检查定义了
f::(Floating a,Integral a)类型的函数=>a->a
——但我不知道哪种类型的
a
适合这个账单。:-}
sum [sqrt $ fromIntegral x | x <- [10..100], x `mod` 10 == 0]