我知道如何檢查是否字符串包含特定字符像這樣的一個人物:檢查一個字符串包含使用遞歸
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