文字列間の距離
2つの文字列の距離を求める関数。挿入、入れ替え、削除のいずれかの操作を1回行うと距離が1増える。
import Control.Monad distance :: String -> String -> [Int] distance [] [] = return 0 distance xs [] = return $ length xs distance [] ys = return $ length ys distance (x:xs) (y:ys) | x == y = distance xs ys | otherwise = msum [ map (+1) $ distance xs (y:ys) -- insert , map (+1) $ distance xs ys -- change , map (+1) $ distance (x:xs) ys -- delete ]