3 ms·
I have implemented KMP in Haskell. This version doesn't use any index! It is built purely functionally by realizing KMP's failure table is just a finite state a
by chaoxu 12y ago
I have implemented KMP in Haskell. This version doesn't use any index! It is built purely functionally by realizing KMP's failure table is just a finite state automaton(well, almost...) However it is much longer than the C++ version...
Code: https://github.com/Mgccl/haskell-algorithm/blob/master/KMP.hs https://github.com/Mgccl/haskell-algorithm/blob/master/KMP.h...
Description: http://www.chaoxuprime.com/posts/2014-04-11-the-kmp-algorithm-in-haskell.html http://www.chaoxuprime.com/posts/2014-04-11-the-kmp-algorith...
Actually, KMP is a little harder to program purely functionally than the MP algorithm.
An extremely elegant MP algorithm is implemented here:
http://twanvl.nl/blog/haskell/Knuth-Morris-Pratt-in-Haskell http://twanvl.nl/blog/haskell/Knuth-Morris-Pratt-in-Haskell
(Note it says the algorithm is KMP, but it is actually the MP algorithm).
The Aho–Corasick string matching algorithm is a generalization of the MP algorithm. Which I also coded in Haskell inspired by the MP code above.
https://github.com/Mgccl/haskell-algorithm/blob/master/AhoCorasick.hs https://github.com/Mgccl/haskell-algorithm/blob/master/AhoCo...
- platz 12y agoLooks like there's also a KMP implementation here specialized to ByteStrings: http://hackage.haskell.org/package/stringsearch http://hackage.haskell.org/package/stringsearch