Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 在Haskell列表理解中做一些替换_String_List_Haskell_List Comprehension - Fatal编程技术网

String 在Haskell列表理解中做一些替换

String 在Haskell列表理解中做一些替换,string,list,haskell,list-comprehension,String,List,Haskell,List Comprehension,我的问题是,如果我输入一个字符串,包含如下内容:Hello,今天是美好的一天如何去掉空格和标点符号,并用小写字母替换大写字母 我知道如何删除它们,但不知道如何替换它们 还要去掉标点符号 对不起,我不知道如何处理字符串,只有数字 testList xs = [if x = [,|.|?|!] then " " | x<-xs] testList xs=[如果x=[,|.|?|!]那么“| x要去除标点符号,可以使用这样的过滤器 [x | x<-[1..10], x `mod` 2

我的问题是,如果我输入一个字符串,包含如下内容:
Hello,今天是美好的一天如何去掉空格和标点符号,并用小写字母替换大写字母

我知道如何删除它们,但不知道如何替换它们

还要去掉标点符号

对不起,我不知道如何处理字符串,只有数字

testList xs = [if x = [,|.|?|!] then " "  | x<-xs] 

testList xs=[如果x=[,|.|?|!]那么“| x要去除标点符号,可以使用这样的过滤器

[x | x<-[1..10], x `mod` 2 == 0]

[x | x我很久没有在Haskell中编写代码了,但是下面应该删除无效字符(用空格替换),并将字符从大写转换为小写:

import Data.Char

replace invalid xs = [if elem x invalid then ' ' else toLower x | x <- xs]
您可以这样调用
replace
(或
repl
)函数:

replace ",.?!" "Hello, today is a Nice Day!!"
上述代码将返回:

"hello  today is a nice day  "

编辑:我正在使用Haskell中
Data.Char
中的
toLower
函数,但是如果您想自己编写,请检查这里的堆栈溢出。这个问题以前已经问过了。

这是一个没有导入模块的版本,使用fromnum和toEnum选择允许哪些字符:

testList xs = 
  filter (\x -> elem (fromEnum x) ([97..122] ++ [32] ++ [48..57])) $ map toLower' xs 
    where toLower' x = if elem (fromEnum x) [65..90] 
                          then toEnum (fromEnum x + 32)::Char 
                          else x

OUTPUT:
*Main> testList "Hello, today is a Nice Day!!"
"hello today is a nice day"
对于无模块替换功能,类似于以下内容可能会起作用:

myReplace toReplace xs = map myReplace' xs where
  myReplace' x
    | elem (fromEnum x) [65..90] = toEnum (fromEnum x + 32)::Char
    | elem x toReplace           = ' '
    | otherwise                  = x

OUTPUT:
*Main> myReplace "!," "Hello, today is a Nice Day!! 123"
"hello  today is a nice day   123"

您将在
Data.Char中找到所需的函数:

import Data.Char

process str = [toLower c | c <- str , isAlpha c]
如果要将标点符号转换为空格,并将字符从大写转换为小写:

testList xs = [if x `elem` ",.?!" then ' ' else toLower x | x<-xs]
testList2 xs = [toLower x | x<-xs, not (x `elem` ",.?!")]
示例:
testList2“Te..S,!t LiS?t”==“测试列表”

如果您不想要或无法导入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
toLower':Char->Char
托洛尔焦
|isNotUppercase=char--无需更改
|否则=toEnum(codeChar+diffLowerUpperChar)--降低字符数
哪里
codeChar=fromEnum char——每个字符都有一个数字代码
代码A=65
代码_Z=90
代码a=97
isNotUppercase=codeCharcode|Z
diffLowerUpperChar=代码a-代码a

使用应用程序样式

这本书中的一段文字引述了“为伟大的利益向你学习哈斯克尔!”

在列表上使用应用程序样式通常可以很好地替代 列出理解。在第二章中,我们想看到所有 [2,5,10]和[8,10,11]的可能产物,因此我们这样做:

[x*y | x50)$(*)[2,5,10][8,10,11]
-- [55,80,100,110]

那么它会像[x]一样吗|x@BobBobington不,您使用的是更容易阅读的
modx2
x2``mod``2
。它几乎与
1+2
(+)1 2
@Bobbington:你说得对。这就是我用Python编写太多代码的原因:P@kyticka:在问题中,您可以使用缩进。在注释中,您可以用反斜杠转义反斜杠:
\\
->
请记住,OP也想用空格替换某些字符。这并不能解决问题--OP想要若要用空格替换无效字符,请不要删除它们。请参阅@OscarMederos的回答。“如何去掉空格和标点符号,并用小写字母替换大写字母?”这听起来像是OP想要删除空格。如果OP的意思是其他的,也许他可以编辑这个问题以澄清问题。这应该是我回答中的一条评论。你已经看过了吗?这很有帮助。谢谢。教授给我们的power point只有数字示例,但这澄清了我的很多问题操作字母的提示。但我如何实现toLower?它不在范围内,我尝试添加import data.char,但这也给了我一个错误。编辑:我让它工作nvm。
import Data.Char
testList xs = [if x `elem` ",.?!" then ' ' else toLower x | x<-xs]
testList2 xs = [toLower x | x<-xs, not (x `elem` ",.?!")]
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
[ x*y | x <- [2,5,10], y <- [8,10,11]]         
(*) <$> [2,5,10] <*> [8,10,11]
filter (>50) $ (*) <$> [2,5,10] <*> [8,10,11]
-- [55,80,100,110]