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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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中的分析错误(可能是不正确的缩进或不匹配的括号)_List_Haskell - Fatal编程技术网

List 如何修复Haskell中的分析错误(可能是不正确的缩进或不匹配的括号)

List 如何修复Haskell中的分析错误(可能是不正确的缩进或不匹配的括号),list,haskell,List,Haskell,我编写了以下代码,并将错误放在标题中。 有人能帮我吗? 第7行出错 punkteImKreis :: Double -> [(Double, Double)] punkteImKreis k = [(x,y)|x <- [1.0,2.0..k-1.0], y <- [1.0,2.0..k-1.0] ] anteilImKreis :: Double -> Double let l = length(punkteImKre

我编写了以下代码,并将错误放在标题中。 有人能帮我吗? 第7行出错

punkteImKreis :: Double -> [(Double, Double)]
punkteImKreis k = [(x,y)|x <- [1.0,2.0..k-1.0],
                         y <- [1.0,2.0..k-1.0] ]

anteilImKreis :: Double -> Double
let l = length(punkteImKreis)
in anteilImKreis k = (fromIntegral (l)) / k^2

此定义中存在错误:

anteilImKreis :: Double -> Double
let l = length(punkteImKreis)
in anteilImKreis k = (fromIntegral (l)) / k^2
let是一个表达式;因此,它必须位于定义内,即=符号右侧。这应该是:

anteilImKreis :: Double -> Double
anteilImKreis k =
  let l = length(punkteImKreis)
  in (fromIntegral (l)) / k^2
顺便说一句,当函数的参数只是一个标识符时,实际上不需要用括号括起来。我将改写如下:

anteilImKreis :: Double -> Double
anteilImKreis k =
  let l = length punkteImKreis
  in (fromIntegral l) / k^2
此外,这还暴露了另一个错误。punkteImKreis不是一个列表;它是一个返回列表的函数,这意味着不能直接计算它的长度。我想你的意思是:

anteilImKreis :: Double -> Double
anteilImKreis k =
  let l = length (punkteImKreis k)
  in (fromIntegral l) / k^2

错误是什么?@moonGoose他们在标题中写下了错误。小注释:from integral l周围的括号也是多余的,这可以用where块anteilImKreis k=from整数l/k^2写,其中l=length punkteImKreis k,或者不带局部绑定anteilImKreis k=from整数长度punkteImKreis k/k^2@JonPurdy我知道积分l的括号是多余的,但我个人更喜欢这样写,以避免歧义。我也知道它可以使用where或不使用绑定来编写,但我认为这样做与回答这个问题无关。@bradm:当然,只是OP和初学者Haskellers将来遇到的一些补遗。