haskell非详尽模式缺失

时间:2017-12-22 06:07:58

标签: haskell

我收到以下错误,我假设所有的递归案例,缺少什么?

Prelude> product [] =1
Prelude> product (x:xs) =x * product xs
Prelude> product [1,2,3]
*** Exception: <interactive>:48:1-30: Non-exhaustive patterns in function product

2 个答案:

答案 0 :(得分:8)

GHCi分别处理每一行,因此您定义了一个函数

Query : select count(*) from tests3;

Vertex failed, vertexName=Map 1, vertexId=vertex_1513582536692_0022_1_00
, diagnostics=[Vertex vertex_1513582536692_0022_1_00 [Map 1] killed/failed due to:ROOT_INPUT_INIT_FAILURE, Vertex Input: tests3 initializer fail
ed, vertex=vertex_1513582536692_0022_1_00 [Map 1], com.amazon.ws.emr.hadoop.fs.shaded.com.amazonaws.services.s3.model.AmazonS3Exception: Bad Req
uest (Service: Amazon S3; Status Code: 400; Error Code: 400 Bad Request; Request ID: DEBA2E241B9DE8C3),

然后通过定义新函数

来遮蔽mkdir("\\\\.\\C:\\path\\to\\con");
product [] = 1

要解决此问题,您可以将productproduct (x:xs) = x * product xs 用于多行广告块:

:{

或者,这就是我的建议,将你的函数定义放在一个文件中并加载到GHCi中。

答案 1 :(得分:2)

我强烈建议您使用-Wall打开警告。在这里,这样做也会暗示每个定义都是单独考虑的事实。

> :set -Wall
> product [] = 1

<interactive>:2:1: warning: [-Wincomplete-patterns]
    Pattern match(es) are non-exhaustive
    In an equation for ‘product’: Patterns not matched: (_:_)
> product (x:xs) = x * product xs

<interactive>:3:1: warning: [-Wincomplete-patterns]
    Pattern match(es) are non-exhaustive
    In an equation for ‘product’: Patterns not matched: []

确实,请注意关于[]的最后警告投诉如何不匹配,表明忽略了第一个定义product []