Since takeWhile
was mentioned in a comment and Haskell has this function, here is how you could achieve the desired result in Haskell:
takeWhile (flip notElem ys) xs
where your example would be
takeWhile (flip notElem "def") "abcdef"
That is, you take elements from the list xs
as long as they are not contained in the list ys
. As soon as you find an element that is contained in ys
(or hit the end of xs
) you stop.
In Standard ML it would be:
fun take_while p [] = []
| take_while p (x::xs) =
if p x then x :: take_while p xs
else []
EDIT: Above, I assumed that the specification was that we stop in the first list, as soon as we find an (arbitrary) element of the second list. Hence the use of takeWhile
. However, from the OP it is not clear to me what the actual specification is. If it is remove an existing suffix (the second list) from the input (the first list), then the solution is of course different. In Haskell, without thinking about efficiency, we could do:
removeSuffix [] ys = []
removeSuffix xs@(z:zs) ys
| xs == ys = []
| otherwise = z : removeSuffix zs ys