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!
相关问题
- Replacing more than n consecutive values in Pandas
- Understanding do notation for simple Reader monad:
- Making Custom Instances of PersistBackend
- Haskell: What is the differrence between `Num [a]
- applying a list to an entered function to check fo
相关文章
- Is it possible to write pattern-matched functions
- Haskell underscore vs. explicit variable
- Top-level expression evaluation at compile time
- Stuck in the State Monad
- foldr vs foldr1 usage in Haskell
- List of checkboxes with digestive-functors
- How does this list comprehension over the inits of
- Replacing => in place of -> in function type signa
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:
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.So, with tuples, you can have your type signature for
replace
as: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.
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
andData.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:
Now, you want to check each character of
text
and if it's equal tofindChr
, you want to replace it withreplaceChr
, otherwise leave it alone.I'll let you fill in the details (hint: if-then-else).
Then, you can use this to build your
replace
function using the simplerreplace1
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.