Bob*_*ton 6 string haskell list-comprehension list
我的问题是,如果我输入一个包含诸如Hello, today is a Nice Day!!
如何摆脱空格和标点符号以及用小写字母替换大写字母的字符串?
我知道如何删除它们但不知道如何替换它们.
还要摆脱标点符号.
对不起,我不知道如何乱用字符串,只有数字.
testList xs = [if x = [,|.|?|!] then " " | x<-xs]
Run Code Online (Sandbox Code Playgroud)
import Data.Char
Run Code Online (Sandbox Code Playgroud)
如果要将标点符号转换为空格,将字符从大写转换为小写:
testList xs = [if x `elem` ",.?!" then ' ' else toLower x | x<-xs]
Run Code Online (Sandbox Code Playgroud)
例: testList "TeST,LiST!" == "test list "
如果要删除标点符号并将字符从大写转换为小写:
testList2 xs = [toLower x | x<-xs, not (x `elem` ",.?!")]
Run Code Online (Sandbox Code Playgroud)
例: testList2 "Te..S,!t LiS?T" == "test list"
如果您不想或不能导入Data.Char,这是toLower的实现:
toLower' :: Char -> Char
toLower' char
| isNotUppercase = char -- no change required
| otherwise = toEnum (codeChar + diffLowerUpperChar) -- char lowered
where
codeChar = fromEnum char -- each character has a numeric code
code_A = 65
code_Z = 90
code_a = 97
isNotUppercase = codeChar < code_A || codeChar > code_Z
diffLowerUpperChar = code_a - code_A
Run Code Online (Sandbox Code Playgroud)