Haskell 如果GHCi不';不声明函数类型?

Haskell 如果GHCi不';不声明函数类型?,haskell,Haskell,我很难理解为什么这是错误的。GHCi并没有显示它的函数类型,也没有抛出一个让我难以调试的错误。原因是什么:1。为什么它没有抛出任何错误2。是什么让这个代码不起作用 :{ let sumCircle [] = error "empty list"; sumCircle [x] = 3.14159*x^2; sumCircle [x:restOfList] = sumCircle x + sumCircle restOfList }: 多行输入的分隔符已交换。您需要:}才能关闭多行

我很难理解为什么这是错误的。GHCi并没有显示它的函数类型,也没有抛出一个让我难以调试的错误。原因是什么:1。为什么它没有抛出任何错误2。是什么让这个代码不起作用

:{
let sumCircle [] = error "empty list";
    sumCircle [x] = 3.14159*x^2;
    sumCircle [x:restOfList] = sumCircle x + sumCircle restOfList
}:

多行输入的分隔符已交换。您需要
:}
才能关闭多行输入,而不是
。请记住,冒号(
)表示GHCi的行内容不是Haskell,而是特定于GHCi的构造

我们还可以在GHCi的提示下看到:

Prelude> :{
Prelude| let sumCircle [] = error "empty list";
Prelude|     sumCircle [x] = 3.14159*x^2;
Prelude|     sumCircle [x:restOfList] = sumCircle x + sumCircle restOfList
Prelude| }:
Prelude|
请注意,它仍然是
Prelude
,而不是
Prelude>
。一旦我们改用
:}
,就会出现由于
}:
:

Prelude| :}

<interactive>:5:1: error:
    parse error (possibly incorrect indentation or mismatched brackets)
前奏曲|:}
:5:1:错误:
分析错误(可能缩进不正确或括号不匹配)
如果我们从一开始就使用正确的分隔符,那么最终会出现预期的类型错误:

Prelude> :{
Prelude| let sumCircle [] = error "empty list";
Prelude|     sumCircle [x] = 3.14159*x^2;
Prelude|     sumCircle [x:restOfList] = sumCircle x + sumCircle restOfList
Prelude| :}

<interactive>:4:56: error:
    * Occurs check: cannot construct the infinite type: t ~ [t]
    * In the first argument of `sumCircle', namely `restOfList'
      In the second argument of `(+)', namely `sumCircle restOfList'
      In the expression: sumCircle x + sumCircle restOfList
    * Relevant bindings include
        restOfList :: [t] (bound at <interactive>:4:18)
        x :: t (bound at <interactive>:4:16)
        sumCircle :: t -> [t] (bound at <interactive>:2:5)
序曲>:{
前奏曲|让sumCircle[]=错误“空列表”;
前奏曲| sumcrourcle[x]=3.14159*x^2;
前奏曲| sumCircle[x:restOfList]=sumCircle x+sumCircle restOfList
前奏曲|:}
:4:56:错误:
*发生检查:无法构造无限类型:t~[t]
*在'sumcycle'的第一个参数中,即'restOfList'
在“(+)”的第二个参数中,即“sumCircle restOfList”
在表达式中:sumCircle x+sumCircle restOfList
*相关绑定包括
restOfList::[t](绑定时间:4:18)
x::t(在4:16绑定)
sumcirle::t->[t](绑定时间:2:5)

顺便说一下,您可以使用
pi
而不是
3.1415926…
,使用
sum
map
您不需要显式递归,但这是为了以后的清理。

您的多行输入分隔符被交换。您需要
:}
才能关闭多行输入,而不是
。请记住,冒号(
)表示GHCi的行内容不是Haskell,而是特定于GHCi的构造

我们还可以在GHCi的提示下看到:

Prelude> :{
Prelude| let sumCircle [] = error "empty list";
Prelude|     sumCircle [x] = 3.14159*x^2;
Prelude|     sumCircle [x:restOfList] = sumCircle x + sumCircle restOfList
Prelude| }:
Prelude|
请注意,它仍然是
Prelude
,而不是
Prelude>
。一旦我们改用
:}
,就会出现由于
}:
:

Prelude| :}

<interactive>:5:1: error:
    parse error (possibly incorrect indentation or mismatched brackets)
前奏曲|:}
:5:1:错误:
分析错误(可能缩进不正确或括号不匹配)
如果我们从一开始就使用正确的分隔符,那么最终会出现预期的类型错误:

Prelude> :{
Prelude| let sumCircle [] = error "empty list";
Prelude|     sumCircle [x] = 3.14159*x^2;
Prelude|     sumCircle [x:restOfList] = sumCircle x + sumCircle restOfList
Prelude| :}

<interactive>:4:56: error:
    * Occurs check: cannot construct the infinite type: t ~ [t]
    * In the first argument of `sumCircle', namely `restOfList'
      In the second argument of `(+)', namely `sumCircle restOfList'
      In the expression: sumCircle x + sumCircle restOfList
    * Relevant bindings include
        restOfList :: [t] (bound at <interactive>:4:18)
        x :: t (bound at <interactive>:4:16)
        sumCircle :: t -> [t] (bound at <interactive>:2:5)
序曲>:{
前奏曲|让sumCircle[]=错误“空列表”;
前奏曲| sumcrourcle[x]=3.14159*x^2;
前奏曲| sumCircle[x:restOfList]=sumCircle x+sumCircle restOfList
前奏曲|:}
:4:56:错误:
*发生检查:无法构造无限类型:t~[t]
*在'sumcycle'的第一个参数中,即'restOfList'
在“(+)”的第二个参数中,即“sumCircle restOfList”
在表达式中:sumCircle x+sumCircle restOfList
*相关绑定包括
restOfList::[t](绑定时间:4:18)
x::t(在4:16绑定)
sumcirle::t->[t](绑定时间:2:5)

顺便说一下,您可以使用
pi
而不是
3.1415926…
,使用
sum
map
不需要显式递归,但这是为了以后的清理。

结束标记是
:}
,而不是
}:
。冒号实际上并不是封装的一部分,它用于“执行GHCi命令”。它仍在等待您使用
:}
关闭块。关闭标记是
:}
,而不是
。冒号实际上并不是封装的一部分,它用于“执行GHCi命令”。它仍在等待您使用
:}
关闭该块。啊,非常感谢您对>和|的深入了解。这在将来会很有帮助。很抱歉问了一个愚蠢的问题,但是你会如何改变最后一句话来让事情顺利进行呢?事实上我不明白我写的是什么导致了issue@HayashiEsme同时有两个错误。首先,您需要括号,例如
sumcirle(x:restOfList)
。否则,您将匹配列表中的列表,例如
[[1,2,3,4,5]
,其中
x
1
[2,3,4,5]
。接下来,最后一行中的
sumCircle x
期望
x
是一个列表。但这将使
restOfList
成为一个列表列表,这将产生
x
作为列表列表列表,这将使
restOfList
成为一个列表列表列表,您可以得到它
sumCircle
仅适用于列表,但您尝试在单个元素上使用它。如果您使用显式类型签名,它会变得更加清晰。啊,非常感谢您对>和|的深入了解。这在将来会很有帮助。很抱歉问了一个愚蠢的问题,但是你会如何改变最后一句话来让事情顺利进行呢?事实上我不明白我写的是什么导致了issue@HayashiEsme同时有两个错误。首先,您需要括号,例如
sumcirle(x:restOfList)
。否则,您将匹配列表中的列表,例如
[[1,2,3,4,5]
,其中
x
1
[2,3,4,5]
。接下来,最后一行中的
sumCircle x
期望
x
是一个列表。但这将使
restOfList
成为一个列表列表,这将产生
x
作为列表列表列表,这将使
restOfList
成为一个列表列表列表,您可以得到它
sumCircle
仅适用于列表,但您尝试在单个元素上使用它。如果使用显式类型签名,它会变得更加清晰。