Skip to content

Instantly share code, notes, and snippets.

@nkaretnikov
Last active August 29, 2015 14:17
Show Gist options
  • Save nkaretnikov/f2264ba6089c213be449 to your computer and use it in GitHub Desktop.
Save nkaretnikov/f2264ba6089c213be449 to your computer and use it in GitHub Desktop.
Subseq
subseq :: Eq a => [a] -> [a] -> [a]
subseq xs ys = let res = go xs ys
in if res /= xs then [] else res
where
go _ [] = []
go [] _ = []
go xs@(x:xt) ys@(y:yt)
| x == y = x : go xt yt
| otherwise = go xs yt
subseq123 = subseq [1,2,3]
testsEq = map subseq123 $
[ [1,2,3]
, [1,1,1,2,2,3]
, [1,2,7,3]
, [5,6,1,9,9,2,7,3,8]
, [5,6,1,9,9,2,7,3]
]
testsNotEq = map subseq123 $
[ [1,2]
, [1,3]
, [5,6,2,1,7,3,8]
]
tests = all (==[1,2,3]) testsEq
&& all (==[]) testsNotEq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment