From baa7ab145907c7959561bafdd667e1bac1722cef Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 18 Jan 2019 11:16:19 -0800 Subject: [PATCH] Add fold-based solution for isBST. Thanks random guy in the front for the idea. --- HW1.fedorind.hs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/HW1.fedorind.hs b/HW1.fedorind.hs index 6456dca..0cc2240 100644 --- a/HW1.fedorind.hs +++ b/HW1.fedorind.hs @@ -191,12 +191,7 @@ inorder = treeFoldr (:) [] -- True -- isBST :: Tree -> Bool -isBST (Leaf _) = True -isBST (Node i l r) = i >= value l && i <= value r && isBST l && isBST r - where - value (Leaf i) = i - value (Node i _ _) = i - +isBST tree = snd $ treeFoldl (\v (p, b) -> (v, b && v >= p)) (minInt tree, True) tree -- | Check whether a number is contained in a binary search tree. -- (You may assume that the given tree is a binary search tree.)