简体   繁体   English

为什么我的 Guard 不想在 Haskell 中调用递归函数

[英]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.我试图在我的 Haskell 程序中调用递归。 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.在 x:xs 中有我的单词列表,在我的 b 中,我有一个函数,它的工作是改变单词的位置。

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.基本上,我的问题是我的xs列表。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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