Haskell non exhaustive patterns in function

前端 未结 3 1288
粉色の甜心
粉色の甜心 2020-12-20 17:42

the following code compiles well but when i try to input helper 10 primes [] [] it gives me :Non-exhaustive patterns in function helper

primes = [2, 3, 5, 7,         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-20 18:15

    What you're trying to evaluate:

    helper 10 primes   []  []
    

    Your equations for helper:

    helper n  []       (v) _      = ...
    helper n  (x:y:xs) (v) (c:cs) = ...
    

    Let's try to match the first equation:

    • n = 10 matches
    • [] = primes does not match

    OK, let's try to match the second equation instead:

    • n = 10 matches
    • (x:y:xs) = primes matches, with
      • x = 2
      • y = 3
      • xs = [5, 7, 11, 13, ...]
    • v = [] matches
    • (c:cs) = [] does not match

    So neither pattern matches. We have a pattern match failure.

提交回复
热议问题