Haskell函数中的非穷举模式 [英] Non-exhaustive patterns in Haskell function

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

问题描述

我有一个haskell程序的问题。我想做这样的事情:

  main = do 
print $ map foo [(1,[(2 ,3),(4,5)])]

foo ::(Int,[(Int,Int)]) - > (Int,[(Int,Int)])
foo(a,[(b,c)])=(a + 1,[(b,c)])

 <$ c 

$ c>函数Main.foo中的非穷举模式

如何做出这样的动作?
我只想访问不在列表中的参数。

解决方案

( a,[(b,c)])不符合(1,[(2,3),(4,5)]) ,因为后者中的列表有两个元素,而你的模式只需要一个元素。



如果你想保持列表不变,使用这个模式:

  foo(a,bar)=(a + 1,bar)
pre
$ b $现在 bar 将匹配 [(2,3),(4,5) )] ,因为它只是一个绑定,它可以匹配正确类型的任何东西。


I have a problem with a haskell program. I want to do something like this:

main = do
    print $ map foo [(1, [(2, 3), (4,5)])]

foo :: (Int, [(Int, Int)]) -> (Int, [(Int, Int)])
foo (a, [(b, c)]) = (a+1, [(b, c)])

Then i get the run-time error:

Non-exhaustive patterns in function Main.foo

How is it possible to make such a action? I just want to access the parameters which are not in the list.

解决方案

(a, [(b, c)]) does not match (1, [(2, 3), (4, 5)]), because the list in the latter has two elements while your pattern requires there to be only one.

If you want to leave the list unchanged, use this pattern instead:

foo (a, bar) = (a+1, bar)

Now bar will match [(2, 3), (4, 5)] because it is just a binding which will match anything of the correct type.

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

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