[Haskell-cafe] Substring replacements
bmaxa at hotmail.com
Mon Dec 12 10:28:12 EST 2005
>From: Daniel Fischer <daniel.is.fischer at web.de>
>To: "Branimir Maksimovic" <bmaxa at hotmail.com>
>CC: Haskell-Cafe at haskell.org
>Subject: Re: [Haskell-cafe] Substring replacements
>Date: Mon, 12 Dec 2005 16:15:46 +0100
> > Sorry, but
> > Prelude SearchRep> searchReplace "abaaba" "##" "abababaaba"
> > "abababaaba"
> > I haven't analyzed the algorithm, so I don't know why exactly this
> > I'll take a look sometime soon.
>I found the problem (one at least).
>Say the pattern to be replaced begins with 'a' and we have a sufficiently
>match with the pattern starting at the first 'a' in the String. Upon
>encountering the second 'a', while the first pattern still matches, you
>pushing onto the rollback-stack. But that isn't inspected anymore, so if
>actual occurence of the pattern starts at the third (or fourth, n-th)
>occurence of 'a' and that is already pushed onto the rollback, you miss it.
I've corrected this with adjusting rollback position. if rollBack is null
search for rollback starts at second character if not starts at same as
character because I skip what was searched. That's all.
Though I'm not so sure now when I read this.
>So the question is, can we find a cheap test to decide whether to use KMP
In real world situation your KMP will always be fastest on average.
I like that we are not using C arrays as then we have advantage
of lazyness and save on memory usage. C++ program will be faster
on shorter strings but on this large strings will loose due memory
latency. and with your test, both programs are very fast.
Express yourself instantly with MSN Messenger! Download today it's FREE!
More information about the Haskell-Cafe