module Exercise_4 where import Control.Arrow import Data.Bool import Data.List import Data.Maybe {-H4.1.10-} editDistance :: Eq a => [a] -> [a] -> Int editDistance a b = d !! length a !! length b where d = map (\i -> map (\j -> if (min i j == 0) then max i j else minimum $ (d !! (i - 1) !! j + 1) : (d !! i !! (j - 1) + 1) : (bool (d !! (i - 1) !! (j - 1) + 1) (d !! (i - 1) !! (j - 1)) $ a !! (i - 1) == b !! (j - 1)) : (bool [] [d !! (i - 2) !! (j - 2) + 1] $ i > 1 && j > 1 && a !! (i - 1) == b !! (j - 2) && a !! (i - 2) == b !! (j - 1))) [0 ..]) [0 ..] {-H4.1.11-} {-WETT-} spellCorrect :: [String] -> [String] -> [[String]] spellCorrect ws = map minEdit where minEdit x = map fst $ filter ((== minDist) . snd) mapped where mapped = map (id &&& editDistance x) ws minDist = minimum $ map snd mapped {-TTEW-}