简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM