
How about something like this?
import Data.List
findMatch xs ys k = lookup k . concat $ zipWith zip (substrings xs)
(substrings ys)
where substrings = nonempty . map (nonempty . inits) . tails
where nonempty = filter (not . null)
On 20/09/06, Matthias Fischmann
... 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@haskell.org From: Carajillu
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
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
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux)
iD8DBQFFER17TXPx/Y0ym6oRAvNZAKCrLeJQxP0PjJAOz2KDi/S0hi7/ywCeMOfH XIOJJcMs9yFsg2IajkmHX7Y= =+bkI -----END PGP SIGNATURE-----
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe