12章最後の問題
data Tree a = Leaf | Node (Tree a) a (Tree a) deriving Show
という型に対してrepeat,takeを実装する
data Tree a = Leaf | Node (Tree a) a (Tree a) deriving Show
repeatTree :: a -> Tree a
repeatTree x = Node (repeatTree x) x (repeatTree x)
takeTree :: Int -> Tree a -> Tree a
takeTree 0 _ = Leaf
takeTree (n+1) Leaf = Leaf
takeTree (n+1) (Node a b c) = Node (takeTree n a) b (takeTree n c)
replicateTree :: Int -> a -> Tree a
replicateTree n = takeTree n . repeatTree
型に対して適切に実装すればリストに対して操作するように自然に扱える。