在这段代码中有重复的片段:
insert x (AATree t) = case insert' x t of
Same t -> AATree t
Inc t -> AATree t
insertBlack :: (Ord a) => a -> AANode Black (Succ n) a -> AnyColor (Succ n) a
insertBlack x (Black l y r)
| x < y = case insert' x l of
Same l' -> AnyColor $ Black l' y r
Inc l' -> AnyColor $ skew l' y r
| otherwise = case insert' x r of
Same r' -> AnyColor $ Black l y r'
Inc r' -> AnyColor $ Red l y r'
所以很想写一个函数:
insert2 same inc x l = case insert' x l of
Same aa -> same aa
Inc aa -> inc aa
并在任何地方使用它,例如:
insert x (AATree t) = insert2 AATree AATree x t
有没有办法写insert2
?天真的方法不进行类型检查。