我知道如何检查是否字符串包含特定字符像这样的一个人物:检查一个字符串包含使用递归
charFound :: Char -> String -> Bool
charFound c s = c `elem` s
现在,我怎么可能有使用递归同样的办法?
此外,使用模式匹配检查,如果其中一个参数是空的,我越来越
分析错误图案:'的
charFound:: Char->String->Bool
charFound '' s = error "Something is empty." -- ERROR
charFound c "" = error "Something is empty." -- OK
我可以在使用_
忽略一个不是列表的东西的参数?
更新目前代码
charFound :: Char->String->Bool
charFound c (x:xs)
| c == x = True
| xs == "" = False
| otherwise = contido c xs
而且
charFound :: Char->String->Bool
charFound _ "" = False -- returns false if I type > charFound 'a' "Leh"
charFound c (x:xs)
| c == x = True
| otherwise = contido c xs
'elem'是一个递归函数。你可以尝试实现它,或者检查文档是如何实现的。 – duplode
来自LearnYouaHaskell.com:递归实际上是一种定义函数的方法,其中函数应用于其自己的定义中 – PlayHardGoPro
您错过了空列表模式匹配,而是对空列表中的空列表进行了一次有趣的短信测试非空案件。 – dfeuer