Created
January 19, 2015 03:39
-
-
Save jfischoff/d40dec6e90f528d06b79 to your computer and use it in GitHub Desktop.
Simple Diff
This file contains 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
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