Created
April 12, 2012 18:39
-
-
Save supki/2369957 to your computer and use it in GitHub Desktop.
Some stuff related to http://deni-ok.livejournal.com/38228.html
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
import Test.QuickCheck | |
import Tree | |
prop_id [] = True | |
prop_id xs = xs == (toList . fromList) xs | |
prop_elem x xs = (x `elem` xs) == (x `elemTree` fromList xs) |
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 Tree a = Branch a (Tree a) (Tree a) | |
| Empty | |
instance Show a => Show (Tree a) where | |
show = show . toList | |
toList :: Tree a -> [a] | |
toList = go . return | |
where go :: [Tree a] -> [a] | |
go ((Branch a l r):xs) = a : go (xs ++ [l,r]) -- Here we go massive BFSing. | |
go _ = [] | |
fromList :: [a] -> Tree a | |
fromList (x:xs) = Branch x (fromList l) (fromList r) | |
where (l, r) = go xs [] [] 1 | |
go [] al ar _ = (al, ar) | |
go ys al ar k = go (drop (2*k) ys) (al ++ take k ys) (ar ++ take k (drop k ys)) (2*k) | |
fromList [] = Empty | |
elemTree :: Eq a => a -> Tree a -> Bool | |
elemTree x t = x `elem` toList t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment