我在声明查找我的首字母缩略词的条件时遇到问题。我写的条件是:
但是当遇到像“apple apple”这样的字母时,它会返回“apape”,因为我在处理第三个条件时遇到了问题。
acronym :: String -> String
acronym [] = []
acronym [x] = [x]
acronym (x:y:xs)
| x == ' ' = y : acronym xs -- remove if 'head' is blank(space)
| y == ' ' = acronym xs -- remove both x and y if y is blank(space)
| x `elem` ['a'..'z'] || x `elem` ['A'..'Z'] && y `elem` ['a'..'z'] || y `elem` ['A'..'Z']
= x : acronym xs
| x == ' ' && y `elem` ['a'..'z'] || y `elem` ['A'..'Z'] = y :acronym xs
| otherwise = x : y : acronym xs
Run Code Online (Sandbox Code Playgroud)
一些一般性建议:
即使不是简单地调用该words函数来为您完成大部分工作,编写您自己的函数来从字符串中分离出单个单词也有很大帮助。你可以这样做,例如基于空白和模式匹配,比如
splitOffWord :: String -> (String, String)
splitOffWord "" = ("", "")
splitOffWord (' ':cs) = ("", cs)
splitOffWord (c:cs) = case splitOffWord cs of
(word, rest) -> (c:word, rest)
Run Code Online (Sandbox Code Playgroud)
...或基于字母度和标准功能
import Data.Char (isAlpha)
splitOffWord = span isAlpha . dropWhile (not . isAlpha)
Run Code Online (Sandbox Code Playgroud)
...或介于两者之间。
然后,在您的acronym函数中,您可以拆分第一个整个单词,使用它来获取首字母缩略词的第一个字符,并递归输入的其余部分。
acronym (' ':str) = acronym str
acronym str = case splitOffWord str of
(word, rest) -> _ : _
Run Code Online (Sandbox Code Playgroud)
...填写_左边作为练习。