在Haskell中查找并替换 [英] Find and replace in Haskell

查看:75
本文介绍了在Haskell中查找并替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想输入一个包含2个元素的字符列表(仅字母),其中第一个元素是字符串中的字母(findAndReplace的第二个参数),第二个是我想要将其更改为的元素.Haskell中已经存在执行类似功能的功能吗?因为这将极大地帮助您!

I want to input a list of 2 element lists of characters (just letters) where the first element is a letter in a String (the second argument for findAndReplace) and the second is what I want it changed to. Is there already a function in Haskell that does a similar thing? Because this would help greatly!

推荐答案

听起来更像是您可能想使用元组列表而不是列表列表来进行首次输入,因为您指定了固定长度.元组是固定长度的集合,可以具有混合类型,而列表是单一类型的任意长度的集合:

It sounds more like you might want to use a list of tuples instead of a list of lists for your first input, since you specify a fixed length. Tuples are fixed-length collections that can have mixed types, while lists are arbitrary-length collections of a single type:

myTuple = ('a', 'b') :: (Char, Char)
myTriple = ('a', 'b', 'c') :: (Char, Char, Char)
myList = ['a'..'z'] :: [Char]

注意我必须如何指定元组的每个字段的类型.另外,(Char,Char)(Char,Char,Char)的类型不相同,它们不兼容.

Notice how I have to specify the type of each field of the tuples. Also, (Char, Char) is not the same type as (Char, Char, Char), they are not compatible.

因此,通过元组,您可以将 replace 的类型签名设置为:

So, with tuples, you can have your type signature for replace as:

replace :: [(Char, Char)] -> String -> String

现在使用类型签名指定它必须是要查找和替换的成对字符列表,您将不必处理错误的输入,例如,如果有人只提供了一个要搜索的字符而没有一个替换它.

And now this specifies with the type signature that it has to be a list of pairs of characters to find and replace, you won't have to deal with bad input, like if someone only gave a character to search for but not one to replace it with.

我们现在正在传递通常称为关联列表的内容,Haskell甚至在 Data.List Data.Map 中提供了一些内置函数来处理它们.代码>.但是,对于本练习,我认为我们不需要它.

We now are passing in what is commonly referred to as an association list, and Haskell even has some built in functions for dealing with them in Data.List and Data.Map. However, for this exercise I don't think we'll need it.

现在,您希望使用成对的列表来解决此问题,但是如果仅使用一对就可以解决问题:

Right now you're wanting to solve this problem using a list of pairs, but it'd be easier if we solved it using just one pair:

replace1 :: (Char, Char) -> String -> String
replace1 (findChr, replaceChr) text = ?

现在,您要检查 text 的每个字符,如果它等于 findChr ,则要用 replaceChr 替换它,否则别管它.

Now, you want to check each character of text and if it's equal to findChr, you want to replace it with replaceChr, otherwise leave it alone.

replace1 (findChr, replaceChr) text = map (\c -> ...) text

我将让您填写详细信息(提示:if-then-else).

I'll let you fill in the details (hint: if-then-else).

然后,您可以使用它通过更简单的 replace1 函数来构建 replace 函数.这应该可以帮助您入门,如果一两天后仍然无法解决问题,请在下面评论,我会给您另一个提示.

Then, you can use this to build your replace function using the simpler replace1 function. This should get you started, and if you still can't figure it out after a day or two, comment below and I'll give you another hint.

这篇关于在Haskell中查找并替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆