searching for a substring

Hal Daume III hdaume@ISI.EDU
Wed, 7 May 2003 17:18:12 -0700 (PDT)


I think you want something like:

(import List)

> stringContains s = any (s `isPrefixOf`) . tails

then we get:

Prelude List> stringContains "hello" "hell"
False
Prelude List> stringContains "hello" "hello"
True
Prelude List> stringContains "llo" "hello"
True


--
 Hal Daume III                                   | hdaume@isi.edu
 "Arrest this man, he talks in maths."           | www.isi.edu/~hdaume

On Thu, 8 May 2003, sashan wrote:

> Dean Herington wrote:
> 
> >On Thu, 8 May 2003, sashan wrote:
> >
> >  
> >
> >>Hi
> >>
> >>I've written a function that checks for a substring in a given string. 
> >>This seems like a common thing to do and was wondering if there is a 
> >>function that does this in one of the standard modules.
> >>
> >>stringContains :: String -> String -> Bool
> >>stringContains _ [] = True
> >>stringContains [] _ = False
> >>stringContains (h:t) (h1:t1) = if h == h1 then stringContains t t1 else 
> >>stringContains t (h1:t1)
> >>    
> >>
> >
> >Do you intend the following behavior?
> >
> >*Main> "x.y.z" `stringContains` "xyz"
> >True
> >
> >
> >  
> >
> 
> Nope
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>