Created
January 26, 2020 14:59
-
-
Save oisdk/0ffbe729fddaad5fe5e3561a562ff5a0 to your computer and use it in GitHub Desktop.
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 Data.Tree.Binary.Preorder (Tree(..), drawTree) | |
import Control.Arrow (first) | |
import Data.List (unfoldr) | |
fromList :: [a] -> Tree a | |
fromList xs = head (l (foldr f b xs 1 2)) | |
where | |
b _ _ ys zs = (repeat Leaf, (repeat Leaf, ys)) | |
l k = let (xs, ys) = uncurry k ys in xs | |
f x k 0 j ys zs = ([], (l (f x k j (j*2)), ys)) | |
f x k i j ~(y:ys) ~(z:zs) = first (Node x y z:) (k (i-1) j ys zs) | |
prop :: [Int] -> Property | |
prop xs = toList (fromList xs) === xs | |
newtype B a = B { unB :: (B a -> B a) -> (B a -> B a) -> [a] } | |
toList :: Tree a -> [a] | |
toList r = unB (f r b) id id | |
where | |
f (Node x l r) fw = B (\bwl bwr -> x : unB fw (bwl . f l) (bwr . f r)) | |
f Leaf fw = B (\bwl bwr -> []) | |
b = B (\qsl qsr -> unB (qsl (qsr b)) id id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment