Jacquob Cast
Jacquob Cast

Reputation: 156

Why my Guard doesn't want to call a recursive function in Haskell

i'm trying to call to do recursive in my Haskell program. I want to call my function every time a element of my list is matching with a word.

My code is compiling without probleme but when i'm trying to execute him, i have this eror

haskell: haskell.hs:(27,1)-(30,33): Non-exhaustive patterns in function detect

My function where the problem is :

detect :: [[Char]] -> [[Char]] -> [[Char]]
detect (x:xs) b
         | x == "now" || x == "go" = detect xs (SwapP 0 1 b)
         | x == "stop" = detect xs (SwapP 0 (length b - 1) b)
detect (x:xs) b = detect xs b

In x:xs is have my list of words, and in my b, i have a function who the job is to change the position of the words.

But the recursive in the guards are not working.

The weirdest thing of my problem is when i'm trying to do the same but outside my guards, it's working, if i'm doing

  detect :: [[Char]] -> [[Char]] -> [[Char]]
   detect (x:xs) b = detect xs (SwapP 0 (length b - 1) b)

it's working, my first and my last words are changing.

So if anyone have a idea of the problem, you can put a little message. Thanks.

Upvotes: 0

Views: 256

Answers (1)

Jacquob Cast
Jacquob Cast

Reputation: 156

Basically, my problem was my xs list. I was reading all the list recursively and deleting the elements. At the end, my list was without element but my function was still trying to delete & check the content. So, to deal with that, i just added a simple

compares [] b = b

Upvotes: 1

Related Questions