From 34d130005be780756b9c6915a7c188a756e03882 Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 31 May 2017 00:32:37 -0400 Subject: finish vol 8 --- 70b-73/70.hs | 16 ++++++++++++++++ 70b-73/70b.hs | 3 +++ 70b-73/70c.hs | 7 +++++++ 70b-73/71.hs | 6 ++++++ 70b-73/72.hs | 5 +++++ 70b-73/73.hs | 7 +++++++ 6 files changed, 44 insertions(+) create mode 100644 70b-73/70.hs create mode 100644 70b-73/70b.hs create mode 100644 70b-73/70c.hs create mode 100644 70b-73/71.hs create mode 100644 70b-73/72.hs create mode 100644 70b-73/73.hs (limited to '70b-73') 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) ++ ")" -- cgit v1.2.3-70-g09d2