Real World Haskell - 第3章 Exercises Part1
Real World Haskell で Haskell を継続的に勉強中。
Defining Types, Streamlining Functions
Chapter 3. Defining Types, Streamlining Functions
Exercises (Section "Recursive types" の最後にあるもの)
1. Write the converse of fromList for the List type: a function that takes a List a and generates a [a].
2. Define a tree type that has only one constructor, like our Java example. Instead of the Empty constructor, use the Maybe type to refer to a node's children.
1
data List a = Cons a (List a) | Nil deriving (Show) --fromList (x:xs) = Cons x (fromList xs) --fromList [] = Nil test1 = Nil test2 = Cons 0 Nil test3 = Cons 1 (Cons 0 Nil) test4 = Cons 2 (Cons 1 (Cons 0 Nil)) test5 = Cons 3 (Cons 2 (Cons 1 (Cons 0 Nil))) toList (Cons x xs) = x : (toList xs) toList Nil = []
GHCi, version 6.10.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer ... linking ... done. Loading package base ... linking ... done. Prelude> :load ex3--1.hs [1 of 1] Compiling Main ( ex3--1.hs, interpreted ) Ok, modules loaded: Main. *Main> toList test1 [] *Main> toList test2 [0] *Main> toList test3 [1,0] *Main> toList test4 [2,1,0] *Main> toList test5 [3,2,1,0]
2
{- data Maybe a = Just a | Nothing data Tree a = Node a (Tree a) (Tree a) | Empty deriving (Show) simpleTree = Node "parent" (Node "left child" Empty Empty) (Node "right child" Empty Empty) -} data Tree a = Node a (Maybe (Tree a)) (Maybe (Tree a)) deriving (Show) test1 = Node "parent" Nothing Nothing test2 = Node "parent" Nothing (Just (Node "right child" Nothing Nothing)) test3 = Node "parent" (Just (Node "left child" Nothing Nothing)) Nothing test4 = Node "parent" (Just(Node "left child" Nothing Nothing)) (Just(Node "right child" Nothing Nothing)) test5 = Node "parent" (Just(Node "left child" (Just(Node "grandchild" Nothing Nothing)) (Just(Node "grandchilde" Nothing Nothing)))) (Just(Node "righit child" (Just(Node "grandchild" Nothing Nothing)) (Just(Node "grandchilde" Nothing Nothing))))
GHCi, version 6.10.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer ... linking ... done. Loading package base ... linking ... done. Prelude> :load ex3--2.hs [1 of 1] Compiling Main ( ex3--2.hs, interpreted ) Ok, modules loaded: Main. *Main> :info Tree data Tree a = Node a (Maybe (Tree a)) (Maybe (Tree a)) -- Defined at ex3--2.hs:13:5-8 instance (Show a) => Show (Tree a) -- Defined at ex3--2.hs:14:24-27 *Main> test1 Node "parent" Nothing Nothing *Main> test2 Node "parent" Nothing (Just (Node "right child" Nothing Nothing)) *Main> test3 Node "parent" (Just (Node "left child" Nothing Nothing)) Nothing *Main> test4 Node "parent" (Just (Node "left child" Nothing Nothing)) (Just (Node "right chil d" Nothing Nothing)) *Main> test5 Node "parent" (Just (Node "left child" (Just (Node "grandchild" Nothing Nothing) ) (Just (Node "grandchilde" Nothing Nothing)))) (Just (Node "righit child" (Just (Node "grandchild" Nothing Nothing)) (Just (Node "grandchilde" Nothing Nothing) ))) *Main>