错误:Haskell中的非穷尽模式 [英] Error: Non-exhaustive patterns in Haskell

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

问题描述

我有一些代码来处理列表(代表一个矩阵)。每个单元格是 N B 。我想为每一行代表矩阵,数量为 N s。例如: [[B,B,N],[N,N,N]] [1,3]

这是我的代码:

  data Case = B | N导出(Show,Eq)
type Grille = [[Case]]

g0,g1 :: Grille
g0 = [[B,B,B, B,B,B,N],[B,B,N,B,N,B,B],[N,B,N,N,N,N,N],[B,B,B,N, N,B,N],[N,N,N,B,B,B,B],[B,B,B,N,N,B,N]]
g1 = [[B,B ,B,B,B,B,B],[B,B,N,N,N,B,B],[B,B,N,N,N,N,N],[B,B,B ,N,N,N,N],[N,N,N,B,B,B,B],[B,B,B,N,B,B,B]]

projH ::格栅 - > [Int]
projH [[]] = []
projH(x:xg)= testCase(x)++ projH(xg)

testCase :: [Case] - > [int] dans une ligne
testCase [] = [0]
testCase(x:xs)
| x == N = [head(testCase(xs))+ 1]
|否则= testCase(xs)

问题出在函数 projH ,我显然缺少一个模式,但是我使用来处理 [[],...] testCase 函数和 [[]] case。我不知道为什么会给我这个错误:

  *主> projH g0 
[1,2,6,3,3,3 ***例外:devoir.hs:(17,1) - (18,39):函数中的非穷尽模式projH


解决方案

  projH [] = [] 

而不是您的

  projH [[]] = [] 

通过分配 x = [] xg = []


由另一个语句处理

I have some code that deals with a list of lists (that represents a matrix). Each cell is an N or a B. I want to represent the matrix with the number of Ns for each line. For example: [[B,B,N],[N,N,N]] is represented by [1,3].

Here's my code:

data Case = B|N deriving (Show, Eq)
type Grille =[[Case]]

g0,g1 :: Grille
g0 = [[B,B,B,B,B,B,N],[B,B,N,B,N,B,B],[N,B,N,N,N,N,N],[B,B,B,N,N,B,N],[N,N,N,B,B,B,B],[B,B,B,N,N,B,N]]
g1 = [[B,B,B,B,B,B,B],[B,B,N,N,N,B,B],[B,B,N,N,N,N,N],[B,B,B,N,N,N,N],[N,N,N,B,B,B,B],[B,B,B,N,B,B,B]]

projH :: Grille -> [Int]
projH [[]] = []
projH (x:xg) = testCase(x) ++ projH(xg)

testCase :: [Case] -> [Int]dans une ligne
testCase [] = [0]
testCase (x:xs) 
    | x == N = [head(testCase(xs))+1]
    | otherwise = testCase(xs)

The problem is in the function projH, I'm obviously missing a pattern, but I do handle the case with [[],...] with the testCase function and the [[]] case. I don't know why it throws me this error:

*Main> projH g0
[1,2,6,3,3,3*** Exception: devoir.hs:(17,1)-(18,39): Non-exhaustive patterns in function projH

解决方案

projH [] = []

instead of your

projH [[]] = []

As the case you wrote is already handled by the other statement by assigning x = [] and xg = []

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

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