Skip to content

Instantly share code, notes, and snippets.

@jfischoff
Created January 19, 2015 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfischoff/d40dec6e90f528d06b79 to your computer and use it in GitHub Desktop.
Save jfischoff/d40dec6e90f528d06b79 to your computer and use it in GitHub Desktop.
Simple Diff
data Edit a
= Insert a
| Delete a
| Same a
deriving (Show, Eq)
isSame :: Edit a -> Bool
isSame x = case x of
Insert {} -> False
Delete {} -> False
Same {} -> True
diff :: Eq a => [a] -> [a] -> [Edit a]
diff [] ys = map Insert ys
diff xs [] = map Delete xs
diff (x : xs) (y : ys)
| x == y = Same x : diff xs ys
| otherwise =
let dx = diff (x : xs) ys
dy = diff xs (y : ys)
in if length (filter isSame dx) < length (filter isSame dy) then
Delete x : dy
else
Insert y : dx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment