Proposal/Patch: Add findSublistIndex in Data.List

Carlos López Camey c.lopez at kmels.net
Sat Jun 16 12:05:32 CEST 2012


Hello,

I think this function might be of help in Data.List. Why I believe it
would be good to have it in base: `elem` is accompanied by `findIndex`
but `isInfixOf` doesn't have an accompanist.

Cheers,
 Carlos

-- | Receives a sublist and a list. It returns the index where the
sublist appears in the list. For example:
--
-- > findSublistIndex "abc" "abcabbc" == Just 0
-- > findSublistIndex findSublistIndex [5,6] [1..] == Just 4
-- > findSublistIndex "abbc" "abcabbc" == Just 3
-- > findSublistIndex [2,1] [2,4..10] == Nothing
findSublistIndex :: (Eq a) => [a] -> [a] -> Maybe Int
findSublistIndex [] _ = Nothing
findSublistIndex sublist list = findSeqIndex sublist list 0 where
  findSeqIndex _ [] _ = Nothing
  findSeqIndex sublist' list'@(x:xs) i = if (sublist' `isPrefixOf`
list') then Just i else findSeqIndex sublist' xs  (i+1)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Add-Data.List.findSublistIndex.-It-finds-the-index-o.patch
Type: application/octet-stream
Size: 1678 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/libraries/attachments/20120616/4ed7cb65/attachment.obj>


More information about the Libraries mailing list