Function to find a substring

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? _________________________________________________________________ Hotmail has tools for the New Busy. Search, chat and e-mail from your inbox. http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:W...

If you want to use libs to make your life easier, maybe something
along these lines?
Prelude Data.List.Split Safe> fmap length . headMay . split (onSublist
"asdf") $ "blee blah asdf bloo"
Just 10
If they are big strings, it's probably faster to use bytestrings, or
arrays of some kinds, rather than default List implementation of
string.
2010/6/6 R J
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?
________________________________ Hotmail has tools for the New Busy. Search, chat and e-mail from your inbox. Learn more. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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

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?
my quick take, with Maybe and fmap : substringP :: String -> String -> Maybe Int substringP _ [] = Nothing substringP sub str = case isPrefixOf sub str of False -> fmap (+1) $ substringP sub (tail str) True -> Just 0 -- Paul

R J
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@cl.cam.ac.uk

Hi, 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?
The naive algorithm of matching the second string on every position of the first string can be implemented as follows. import Data.List (findIndex, tails) findSubstringIndex text pattern = findIndex (pattern `isPrefixOf`) (tails text) Tillmann

El dom, 06-06-2010 a las 15:51 +0000, R J escribió:
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?
import Data.List f a b = findIndex (a `isPrefixOf`) (tails b) Jürgen
participants (7)
-
Jon Fairbairn
-
Jürgen Doser
-
Paul R
-
R J
-
Thomas Hartman
-
Tillmann Rendel
-
Yitzchak Gale