[Haskell-cafe] Java or C to Haskell

Matthias Fischmann fis at wiwi.hu-berlin.de
Wed Sep 20 06:52:43 EDT 2006


...  and if you want to search strings not single characters:

findmatch s t e = take m . drop n $ t
    where
    m' = length e
    (n, m) = f 0 s
    f i s | take m' s == e  = (i, m')
          | null s          = (0, 0)
          | otherwise       = f (i+1) (tail s)

findmatch "asdfasdf" "asdfxvdf" "fas" == "fxv"

(this one skips equality checks before *and* after the match.  feel
free post the necessary modifications.  :)

matthias



On Wed, Sep 20, 2006 at 02:22:29AM -0700, Carajillu wrote:
> To: haskell-cafe at haskell.org
> From: Carajillu <crespi.albert at gmail.com>
> Date: Wed, 20 Sep 2006 02:22:29 -0700 (PDT)
> Subject: Re: [Haskell-cafe] Java or C to Haskell
> 
> 
> Yes, they must be equal the whole way, I like this recursive solution :)
> 
> Ketil Malde-3 wrote:
> > 
> > Carajillu <crespi.albert at gmail.com> writes:
> > 
> >> compare function just compares the two lists and return true if they are
> >> equal, or false if they are not.
> > 
> >> find_match "4*h&a" "4*5&a" 'h' ----> returns '5' (5 matches with the h)
> >> find_match "4*n&s" "4dhnn" "k" ----> returns ''  (no match at all - lists
> >> are different anyway)
> > 
> > Must they be equal the whole way, or just up to the occurrence of the
> > searched-for character?
> > 
> >   find_match (x:xs) (y:ys) c | x==c = Just y 
> >                              | x/=y = Nothing
> >                              | True = find_match xs ys c
> >   find_match [] [] _ = Nothing
> > 
> > Or, to check the whole list:
> > 
> >   find_match (x:xs) (y:ys) c | x==c && xs == ys = Just y 
> >                              | x/=y = Nothing
> >                              | True = find_match xs ys c
> >   find_match [] [] _ = Nothing
> > 
> > -k
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20060920/eb824a94/attachment.bin


More information about the Haskell-Cafe mailing list