Last active
October 3, 2016 11:08
-
-
Save jackbergus/1c46536e13258bfe4ae3060cb22ff55c 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
data Tree = Leaf | Node Int Tree Tree deriving Show | |
leftmost Leaf x = x | |
leftmost (Node x Leaf _) _ = x | |
leftmost (Node v (Node x y z) _) _ = leftmost (Node x y z) v | |
rightmost Leaf x = x | |
rightmost (Node x _ Leaf) _ = x | |
rightmost (Node v _ (Node x y z)) _ = rightmost (Node x y z) v | |
sorted Leaf = True | |
sorted (Node x Leaf Leaf) = True | |
sorted (Node x (Node y z t) Leaf) = (y<=x) && ((rightmost (Node y z t) x) <= x) && (sorted (Node y z t)) | |
sorted (Node x Leaf (Node y z t)) = (x<y) && ((leftmost (Node y z t) x) > x) && (sorted (Node y z t)) | |
sorted (Node x (Node a b c) (Node d e f)) = (a<=x) && (x>d) && ((rightmost (Node a b c) x) <= x) && ((leftmost (Node d e f) x) > x) && (sorted (Node a b c)) && (sorted (Node d e f)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use as-pattern to not repeat code and to avoid accidental mistakes. For example:
rightmost (Node v _ tr@(Node x y z)) _ = rightmost tr v