Proposal: Add isSubsequenceOf to Data.List

Niklas Hambüchen mail at nh2.me
Mon Oct 13 15:34:17 UTC 2014


Data.List has `subsequences`, calculating all subsequences of a list,
but it doesn't provide a function to check whether a list is a
subsequence of another list.

`isSubsequenceOf` would go into the "Predicates" section
(http://hackage.haskell.org/package/base-4.7.0.1/docs/Data-List.html#g:12)
which already contains:

* isPrefixOf (dual of inits)
* isSuffixOf (dual of tails)
* isInfixOf

With this proposal, we would add

* isSubsequenceOf (dual of subsequences)


Suggested implementation:

-- | `isSubsequenceOf a b`: Checks if a is a subsequence of b.
isSubsequenceOf :: (Eq a) => [a] -> [a] -> Bool
isSubsequenceOf []    _                    = True
isSubsequenceOf _     []                   = False
isSubsequenceOf a@(x:a') (y:b) | x == y    = isSubsequenceOf a' b
                               | otherwise = isSubsequenceOf a b

Niklas


More information about the Libraries mailing list