Is the following an effective way to implement the Levenshtein Distance with Haskell vectors?
import qualified Data.Vector as V
levenshtein s1 s2 = levenshteinV (V.fromList s1) (V.fromList s2)
levenshteinV p1 p2 = lev V.! l1 V.! l2
where
lev = V.map levi (V.enumFromN 0 (l1 + 1))
levi i = V.map (levij i) (V.enumFromN 0 (l2 + 1))
levij i j
| i == 0 = j
| j == 0 = i
| otherwise =
((lev V.! (i - 1) V.! j) + 1) `min`
((lev V.! i V.! (j - 1)) + 1) `min`
((lev V.! (i - 1) V.! (j - 1)) + ind (i - 1) (j - 1))
ind i j = if p1 V.! i == p2 V.! j then 0 else 1
l1 = V.length p1
l2 = V.length p2
In particular, should I be using V.map
to construct the vectors or is there a better approach? Perhaps V.generate
? Or does it not make a difference because of lazy evaluation?