[Haskell-cafe] Function to find a substring

Yitzchak Gale gale at sefer.org
Tue Jun 8 06:13:44 EDT 2010


R J wrote:
>> What's an elegant definition of a Haskell function that takes two strings
>> and returns "Nothing" in case the first string isn't a substring of the
>> first, or "Just i", where i is the index number of the position within the
>> first string where the second string begins?

Thomas Hartman wrote:
> If you want to use libs to make your life easier, maybe something
> along these lines?
> Prelude Data.List.Split Safe> ...

True, those are both very nice libraries. It's just about as
easy to do this with the more standard ones, though:

Prelude Data.List Data.Maybe> listToMaybe . map fst . filter (("asdf"
`isPrefixOf`) . snd) . zip [0..] . tails $ "blee blah asdf bloo"
Just 10

Regards,
Yitz


More information about the Haskell-Cafe mailing list