diff options
author | Determinant <[email protected]> | 2017-05-31 00:32:37 -0400 |
---|---|---|
committer | Determinant <[email protected]> | 2017-05-31 00:32:37 -0400 |
commit | 34d130005be780756b9c6915a7c188a756e03882 (patch) | |
tree | 4a5b1ca11e87086adc081e428561b3230abb38cb | |
parent | e0fff7c4014512bf88b23136dbbb426d9d9b7511 (diff) |
finish vol 8
-rw-r--r-- | 70b-73/70.hs | 16 | ||||
-rw-r--r-- | 70b-73/70b.hs | 3 | ||||
-rw-r--r-- | 70b-73/70c.hs | 7 | ||||
-rw-r--r-- | 70b-73/71.hs | 6 | ||||
-rw-r--r-- | 70b-73/72.hs | 5 | ||||
-rw-r--r-- | 70b-73/73.hs | 7 |
6 files changed, 44 insertions, 0 deletions
diff --git a/70b-73/70.hs b/70b-73/70.hs new file mode 100644 index 0000000..69f42f5 --- /dev/null +++ b/70b-73/70.hs @@ -0,0 +1,16 @@ +data Tree a = Node a [Tree a] deriving (Eq, Show) + +stringToTree :: [Char] -> Tree Char + +stringToTree l = fst $ build l + where build (x:xs) = (Node x chd'', xs'') + where loop ('^':xs) = ([], xs) + loop xs = (chd':c, x) + where (chd', xs') = build xs + (c, x) = loop xs' + (chd'', xs'') = loop xs + + +treeToString :: Tree Char -> [Char] + +treeToString (Node v xs) = v:(concat $ map treeToString xs) ++ "^" diff --git a/70b-73/70b.hs b/70b-73/70b.hs new file mode 100644 index 0000000..dbeb5b5 --- /dev/null +++ b/70b-73/70b.hs @@ -0,0 +1,3 @@ +data Tree a = Node a [Tree a] deriving (Eq, Show) + +-- Haskell is type-safe, so there is no solution. diff --git a/70b-73/70c.hs b/70b-73/70c.hs new file mode 100644 index 0000000..2a10432 --- /dev/null +++ b/70b-73/70c.hs @@ -0,0 +1,7 @@ +import Data.List (foldl') + +data Tree a = Node a [Tree a] deriving (Eq, Show) + +nnodes :: Tree a -> Int + +nnodes (Node _ xs) = 1 + (foldl' (+) 0 $ map nnodes xs) diff --git a/70b-73/71.hs b/70b-73/71.hs new file mode 100644 index 0000000..e99c3fe --- /dev/null +++ b/70b-73/71.hs @@ -0,0 +1,6 @@ +data Tree a = Node a [Tree a] deriving (Eq, Show) + +ipl :: Tree a -> Int + +ipl = ipl' 0 + where ipl' l (Node _ xs) = l + (sum $ map (ipl' (l + 1)) xs) diff --git a/70b-73/72.hs b/70b-73/72.hs new file mode 100644 index 0000000..06f2ec9 --- /dev/null +++ b/70b-73/72.hs @@ -0,0 +1,5 @@ +data Tree a = Node a [Tree a] deriving (Eq, Show) + +bottomUp :: Tree Char -> [Char] + +bottomUp (Node v xs) = (concat $ map bottomUp xs) ++ [v] diff --git a/70b-73/73.hs b/70b-73/73.hs new file mode 100644 index 0000000..0db324e --- /dev/null +++ b/70b-73/73.hs @@ -0,0 +1,7 @@ +import Data.List (intersperse) +data Tree a = Node a [Tree a] deriving (Eq, Show) + +display :: Tree Char -> [Char] + +display (Node v []) = [v] +display (Node v xs) = '(':v:' ':(concat $ intersperse " " $ map display xs) ++ ")" |