Created
June 1, 2016 00:50
-
-
Save sordina/658ca0422ac84bc8c8c38dc82d32d4fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
import Data.HashMap.Lazy | |
import Data.Text hiding (last, minimum) | |
import Prelude hiding (length) | |
levenstein :: Text -> Text -> Int | |
levenstein a b = snd $ last levsl | |
where | |
subo = (-1, -1) | |
delo = (-1, 0) | |
inso = ( 0, -1) | |
offsets = [subo, delo, inso] | |
levsl = [ ((i, j), lev i j) | j <- uptop b, i <- uptop a ] | |
uptop x = [0 .. length x - 1] | |
levsv = fromList levsl | |
off i j = \(oi, oj) -> (i + oi, j + oj) | |
lev 0 j = j | |
lev i 0 = i | |
lev i j | (index a i) == (index b j) = levsv ! off i j subo | |
| otherwise = 1 + minimum [ levsv ! off i j o | o <- offsets] | |
main :: IO () | |
main = print $ levenstein "hello" "world" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment