Reputation: 1742
I need to create a function that takes two parameters. Both of them are list of strings and one is used as a substitution list for the other. So the function would be called like this -
subs ["the","quick","brown","the"] ["a","dog"]
The output would be - "a quick brown dog". So whenever there's is "the" element in the list, the first "the" is replaced with the first element of the substitution list and the second "the" will be substituted with the second element from the substation list. I can use foldr to iterate through the first list but I don't know how to keep track of substitution list so that the element that is already used won't be used again. Any suggestion?
Upvotes: 1
Views: 450
Reputation: 52049
Am I'm correct in understanding that subs
only replaces occurrences of "the"
in the input word list? So:
subs xs ["a", "dog"]
means to substitute the first occurrence of "the"
in xs
with "a"
and the second occurrence with "dog"
?
If so, you should try something along these lines:
subs xs [] = ... -- no more substitutions - the easy case
subs xs (a:as) =
-- we have a substitution
-- if xs begins with "the" then substitute with `a` and continue
-- otherwise ... some sort of recursion ...
Here are the four cases you need to cover:
subs [] [] = ... -- input is empty and no more subs to make
subs [] (a:as) = ... -- input is empty and some extra subs left over
subs (x:xs) [] = ... -- input not empty and no more subs to make
subs (x:xs) (a:as) = ... -- input not empty and still some subs left
If you can provide definitions for all of these cases you are done!
Upvotes: 2