The two functions aren't really all that similar, if you look at them. What is the same are the first three "trivial" cases, but the last case does different things in both functions. You could also leave out the first two cases in either of those functions since they check the same thing on the same lists. So myIsInfixOf
also works like this:
myIsInfixOf :: (Eq a) => [a] -> [a] -> Bool
myIsInfixOf (a:as) [] = False
myIsInfixOf listA listB | helpFunction listA listB = True
| otherwise = myIsInfixOf listA (tail listB)
This really isn't the same as helpFunction
, and doesn't repeat any nontrivial code.
Also you really want the second function to work independently from the first, since you want to check for every position in listB
if every character of listA
matches those in listB
. To do this in a imperative program you would need nested loops, here you need a nested recursion best done with a helper function like you did. The issue is not that listA
would somehow be lost without the helpFunction, the problem is that the algorithm requires helpFunction
to run over the list independently of myIsInfixOf
.
If you want to avoid manually implementing the recursion of myIsInfixOf
by using more functions from the standard library you could use any
and tails
. For helpFunction
probably explicit recursion is the way to go, but you could simplify the whole last case to:
helpFunction (x:xs) (y:ys) = (x == y) && helpFunction xs ys