Skip to content

Instantly share code, notes, and snippets.

@AKST
Last active December 24, 2015 17:59
Show Gist options
  • Save AKST/6839991 to your computer and use it in GitHub Desktop.
Save AKST/6839991 to your computer and use it in GitHub Desktop.
data Tree a = Node a (Tree a) (Tree a) | Empty
tFilter :: (Ord a) => (a -> Bool) -> Tree a -> Tree a
tFilter f tree = impl f tree Empty
where
impl f Empty acc = acc
impl f (Node x l r) acc =
impl f l $
impl f r $
if f x
then incl x acc
else acc
incl :: Ord a => a -> Tree a -> Tree a
incl x Empty = Node x Empty Empty
incl x t@(Node y l r)
| x < y = Node y (incl x l) r
| x > y = Node y l (incl x r)
| otherwise = t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment