功能过滤中的非穷举模式?

时间:2015-10-08 13:18:07

标签: haskell functional-programming

cubes = [ (a,b,c) | a <- [1..30],b <-[1..30],c <- [1..30] ]

filtering (d,f,g)
 | d == f && f == g && d ==g = "cube"

third = filter newfun cubes 

newfun (x,y,z) = (filtering (x,y,z) == "cube")



*Charana> third
[(1,1,1)*** Exception: haskell.hs:(55,1)-(56,37): Non-exhaustive patterns in  function filtering

所以,当我把它放在终端时,它给了我一个non-exhaustive pattern error,他们自己单独的功能工作正常,程序也很好。任何想法? 谢谢

1 个答案:

答案 0 :(得分:3)

请改为尝试:

cubes = [ (a,b,c) | a <- [1..30],b <-[1..30],c <- [1..30] ]

filtering (d,f,g) = d == f && f == g && d == g

third = filter filtering cubes 

一些意见:

你真的需要检查d == g吗?我希望它能从其他两个平等检查中获得传递性。

习惯上将类型注释添加到顶级定义。我建议例如。

cubes :: [(Int,Int,Int)]
cubes = [ (a,b,c) | a <- [1..30],b <-[1..30],c <- [1..30] ]

filtering :: (Int,Int,Int) -> Bool
filtering (d,f,g) = d == f && f == g && d == g

third :: [(Int,Int,Int)]
third = filter filtering cubes 

始终在启用警告的情况下编译代码。例如。在文件顶部使用它

{-# OPTIONS -Wall #-}

或手动将-Wall标志传递给GHC。如果这样做,它会警告您在编译时可能没有详尽的功能。例如,

someFunction x
   | someConditionOn x = someValue

会触发警告,因为它没有说明当someConditionOn x为假时结果应该是什么。