为什么我会在函数中获得“非穷举模式”? [英] Why do I get "non-exhaustive patterns in function"?

查看:92
本文介绍了为什么我会在函数中获得“非穷举模式”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中,Haskell抱怨

函数prime中的非穷举模式



  prime :: Int  - > [int] 
prime x = prime'[​​2..x]其中
prime'(p:ps)= p:prime'[​​x | x< -ps,mod x p> 0&& prime''x [2..div x 2]]
prime''_ [] = True
prime''n(x:xs)
| mod n x == 0 = False
|否则= prime''n xs

prime'[​​] = []

我找不到我的错误。有人可以解释为什么会发生这种情况,它是什么意思?

解决方案

缩进。最后一行定义另一个函数,称为 prime'。因此, prime \prime' prime 的where子句中的定义)没有匹配的模式为空列表。



另外,您的缩进遍布整个地方。你仍然混合标签和空格

In the following code Haskell complains about

Non-exhaustive patterns in function prime'

prime :: Int -> [Int]
prime x = prime' [2..x] where
  prime' (p:ps)= p : prime' [x | x <- ps, mod x p > 0 && prime'' x [2..div x 2]]     
  prime'' _ [] = True
  prime'' n (x:xs)
    | mod n x == 0 = False
    |    otherwise = prime'' n xs

prime' []=[]

I can't find my mistake. Could someone explain why this happens, and what it means?

解决方案

Indentation. The last line defines another function called prime'. Therefore, prime\prime' (the definition in the where clause of prime) doesn't have a matching pattern for the empty list.

Also, you're indentation is all over the place. Do you still mix tabs and spaces?

这篇关于为什么我会在函数中获得“非穷举模式”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆