Haskell:非穷举模式 [英] Haskell: non-exhaustive-patterns

查看:184
本文介绍了Haskell:非穷举模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明天正在为测试进行培训,以完成我对函数式编程的介绍,但有一件事我不明白。



每当我有如下程序时:

  test [] = [] 
test(x:xs)= test(xs)

他所做的是将第一个元素从列表中移出并继续处理。无论何时只剩下一个, xs 应该是 [] ,这又应该触发测试[] = [] 。但是每当我运行这个算法,我得到一个错误。 异常:< interactive>:20:5-16:函数测试中非详尽的模式。



I网上找不到明确的解释。有人请给我一个链接,这是清楚地解释或向我解释? 解决方案

您在问题中发布的代码body 完成详尽的模式匹配。但是,如果您尝试将此定义输入到ghci中,则应该使用单个 let 语句:

  Prelude>让test [] = []; test(x:xs)= test xs 






您正在此处不正确。您首先要定义一个非穷举函数 test

  Prelude> let test [] = [] 

然后您定义另一个非-exhaustive函数,也称为 test ,它隐藏了第一个函数:

 前奏>让test(x:xs)= test xs 


I am training for a test tomorrow to complete my introduction to functional programming but there is one thing I don't understand.

Whenever I have a program like:

test [] = []
test (x:xs) = test (xs)

What he does is that he takes the first element out of the list and continues with the rest. Whenever there is just one left, xs should be [] which in turn should trigger test [] = []. But whenever I run this algorithm I get an error. Exception: <interactive>:20:5-16: Non-exhaustive patterns in function test.

I couldn't find a clear explanation online. Could somebody please send me a link where this is explained clearly or explain it to me?

解决方案

The code you posted in the question's body does exhaustive pattern matching. However, if you try to enter this definition into ghci, you should use a single let statement:

Prelude> let test [] = [] ; test (x:xs) = test xs


What you are doing here is incorrect. You are first defining a non-exhaustive function test:

Prelude> let test [] = []

And then you are defining another non-exhaustive function, also called test, which hides the first one:

Prelude> let test (x:xs) = test xs

这篇关于Haskell:非穷举模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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