find if list 1 is a sequence of list 2 in haskell

Posted by Isaak Wahb on Stack Overflow See other posts from Stack Overflow or by Isaak Wahb
Published on 2012-11-04T16:45:14Z Indexed on 2012/11/04 17:00 UTC
Read the original article Hit count: 136

Filed under:

im trying to check if a given list is a subsequence of another list:

here are example of lists which gives true:

subseq "" "w"
subseq "w" "w"
subseq "ab" "cab"
subseq "cb" "cab"
subseq "aa" "xaxa"
not (subseq "aa" "xax")
not (subseq "ab" "ba")

i just come to this but in some cases it gives a wrong result

subseq :: Eq a => [a] -> [a] -> Bool
subseq [] [] = True
subseq [] ys = True
subseq xs [] = False
subseq (x:xs) (y:ys) = x == y || subseq xs ( 1 `drop` ys )

© Stack Overflow or respective owner

Related posts about haskell