我在声明查找我的首字母缩略词的条件时遇到问题。我写的条件是:
但是当遇到像“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
| …Run Code Online (Sandbox Code Playgroud) myElement [] = []
myElement [h] = [h]
myElement (h:t)
| h == myElement t = True
| otherwise = False
Run Code Online (Sandbox Code Playgroud)
大家好,我正在努力创建自己的elem函数,因为我不允许在 Haskell 中使用任何预定义的函数来做作业。我敢打赌我的代码实际上不起作用,但我想不出其他方式。