[Haskell-cafe] Re: Function to find a substring

Jon Fairbairn jon.fairbairn at cl.cam.ac.uk
Tue Jun 8 04:46:56 EDT 2010


R J <rj248842 at hotmail.com> writes:

> 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?

f n h = listToMaybe [b | (a,b)<- tails h `zip` [1..], n `isPrefixOf` a]

seems plausible, but how do you define elegant?

-- 
Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk




More information about the Haskell-Cafe mailing list