I have been self studying the UPenn Haskell course, and I am unable to get feedback/grading on the assignments complete. I have not done this to check the other homework assignments, but some weird properties of my code made me want to come and check if it is correct. The weird property is the # of elements in my complete and FULLY balanced binary trees; they are the numbers in the Ulam sequence (A081026). This seemed completely counterintuitive to me, so I want to make sure I have correctly coded the generation and examination of these trees.
foldTree :: [a] -> Tree a foldTree (x:xs) = foldr binaryInsert (Node 0 Leaf x Leaf) xs getHeight :: Tree a -> Integer getHeight (Node a _ _ _) = a getHeight Leaf = 0 sameHeight :: Tree a -> Tree a -> Bool sameHeight x y = (getHeight x) == (getHeight y) almostSameHeight :: Tree a -> Tree a -> Bool almostSameHeight x y = abs (getHeight x - getHeight y) <= 1 degreesOfBalanced :: Tree a -> (Tree a -> Tree a -> Bool) -> Bool degreesOfBalanced Leaf _ = True degreesOfBalanced (Node _ lChild _ rChild) comparator = (comparator lChild rChild) && subtreeComparison where subtreeComparison = (degreesOfBalanced lChild comparator) && (degreesOfBalanced rChild comparator) isBalanced :: Tree a -> Bool isBalanced tree = degreesOfBalanced tree sameHeight isAlmostBalanced :: Tree a -> Bool isAlmostBalanced tree = degreesOfBalanced tree almostSameHeight hasLeaf :: Tree a -> Bool hasLeaf (Node a Leaf _ _) = True hasLeaf (Node a _ _ Leaf) = True hasLeaf _ = False isLeaf :: Tree a -> Bool isLeaf Leaf = True isLeaf _ = False replaceLeaf :: Tree a -> a -> Tree a replaceLeaf (Node 0 Leaf nodeVal Leaf) val = Node 1 (Node 0 Leaf val Leaf) nodeVal Leaf replaceLeaf node@(Node height lChild nodeVal rChild) val | isLeaf lChild = Node height (Node 0 Leaf val Leaf) nodeVal rChild | isLeaf rChild = Node height lChild nodeVal (Node 0 Leaf val Leaf) | otherwise = node binaryInsert :: a -> Tree a -> Tree a binaryInsert value Leaf = Node 0 Leaf value Leaf binaryInsert value node@(Node height lChild nodeVal rChild) | hasLeaf node = replaceLeaf node value | not (isBalanced lChild) = Node newHeightL insertedLeft nodeVal rChild | not (isBalanced rChild) = Node newHeightR lChild nodeVal insertedRight | isBalanced node = Node newHeightL insertedLeft nodeVal rChild | otherwise = if getHeight lChild < getHeight rChild then Node newHeightL insertedLeft nodeVal rChild else Node newHeightR lChild nodeVal insertedRight where insertedLeft = binaryInsert value lChild insertedRight = binaryInsert value rChild newHeightL = (max (getHeight insertedLeft) (getHeight rChild)) + 1 newHeightR = (max (getHeight insertedRight) (getHeight lChild)) + 1 testTree = (Node 3 (Node 2 (Node 0 Leaf 'F' Leaf) 'I' (Node 1 (Node 0 Leaf 'B' Leaf) 'C' Leaf)) 'J' (Node 2 (Node 1 (Node 0 Leaf 'A' Leaf) 'G' Leaf) 'H' (Node 1 (Node 0 Leaf 'D' Leaf) 'E' Leaf))) main :: IO() main = print (filter (\x -> snd x) (map (\x -> (x, isBalanced (foldTree [1..x]))) [1..1000]))
I’d also love feedback on the style of it!
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!