Created
March 20, 2023 16:56
-
-
Save trapd00r/98e816b86921da48283bd3f0f3524faa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{- a function that calculates the length of a rope -} | |
length :: Rope -> Int | |
length (Leaf s) = length s | |
length (Node l r) = length l + length r | |
{- a function that calculates the height of a rope -} | |
height :: Rope -> Int | |
height (Leaf s) = 1 | |
height (Node l r) = 1 + max (height l) (height r) | |
{- a function that calculates the weight of a rope -} | |
weight :: Rope -> Int | |
weight (Leaf s) = 1 | |
weight (Node l r) = weight l + weight r | |
{- a function that calculates the depth of a rope -} | |
depth :: Rope -> Int | |
depth (Leaf s) = 0 | |
{- a function that calculates the balance of a rope -} | |
balance :: Rope -> Int | |
balance (Leaf s) = 0 | |
balance (Node l r) = weight l - weight r | |
{- a function that calculates the balance factor of a rope -} | |
balanceFactor :: Rope -> Int | |
balanceFactor (Leaf s) = 0 | |
balanceFactor (Node l r) = abs (balance l) + abs (balance r) | |
{- a function that calculates the average depth of a rope -} | |
averageDepth :: Rope -> Int | |
averageDepth (Leaf s) = 0 | |
averageDepth (Node l r) = (depth l + depth r) `div` 2 | |
{- a function that calculates the average balance factor of a rope -} | |
averageBalanceFactor :: Rope -> Int | |
averageBalanceFactor (Leaf s) = 0 | |
averageBalanceFactor (Node l r) = (balanceFactor l + balanceFactor r) `div` 2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment