I have a question regarding Haskell that's been stumping my brain. I'm currently required to write a function that removes a string i.e. "word"
from a list of strings ["hi", "today", "word", "Word", "WORD"]
returns the list ["hi", "today", "Word", "WORD"]
. I cannot use any higher-order functions and can only resort to primitive recursion.
Thinking about the problem, I thought maybe I could solve it by using a recursion where you search the head of the first string, if it matches "w"
then compare the next head from the tail, and see if that matches "o"
. But then I soon realized that after all that work, you wouldn't be able to delete the complete string "word"
.
My question really being how do I compare a whole string in a list rather than only comparing 1 element at a time with something like: removeWord (x:xs)
. Is it even possible? Do I have to write a helper function to aid in the solution?
Int
first, then it should be just a matter of changing the type signature to make it work for lists of strings. – hammar(x:xs)
against["hi", "today", "word", "Word", "WORD"]
,x
becomes"hi"
andxs
becomes["today", "word", "Word", "WORD"]
. That is, it matches string by string, not character by character. This works because you have a list of strings rather than just one big string. – Tikhon Jelvis