-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.hs
More file actions
17 lines (15 loc) · 709 Bytes
/
Copy pathmain.hs
File metadata and controls
17 lines (15 loc) · 709 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env stack
-- stack --resolver lts-13.7 script
data Tree = Leaf | Node Int Tree Tree deriving Show
checkSort :: Tree -> Bool
checkSort tree = checkSort' tree Nothing Nothing
where checkSort' Leaf _ _ = True
checkSort' (Node value leftTree rightTree) minVal maxVal =
let leftSorted = checkSort' leftTree minVal (Just value)
rightSorted = checkSort' rightTree (Just value) maxVal
in (case minVal of Nothing -> True; Just x -> value >= x) &&
(case maxVal of Nothing -> True; Just x -> value < x) &&
leftSorted && rightSorted
main :: IO ()
main = do
print $ checkSort (Node 2 (Node 1 Leaf Leaf) (Node 3 Leaf Leaf))