Real World Haskell
実戦で学ぶ関数型言語プログラミング
Bryan O'Sullivan, John Goerzen, Don Stewart(著)
山下 伸夫, 伊東 勝利
株式会社タイムインターメディア(翻訳)
開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- プログラミング言語: Haskell (純粋関数型)
Real World Haskell』(Bryan O'Sullivan、John Goerzen、Don Stewart(著)、山下 伸夫、伊東 勝利、株式会社タイムインターメディア(翻訳)、オライリー・ジャパン、2009年、ISBN978-4-87311-423-3)の3章(型を定義し、関数を単純化する)の3.7(再帰型)の練習問題2.を解いてみる。
2.
最初に書いたの
コード(BBEdit)
sample.hs
-- file: sample.hs
data Tree a = Node a (Maybe (Tree a)) (Maybe (Tree a))
deriving (Show)
simpleTree = Node "parent" (Just (Node "left leaf" Nothing Nothing))
(Just (Node "right leaf" Nothing Nothing))
入出力結果(Terminal)
$ ghci
GHCi, version 7.4.2: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :load sample.hs
[1 of 1] Compiling Main ( sample.hs, interpreted )
Ok, modules loaded: Main.
*Main> simpleTree
<interactive>:3:1:
Not in scope: `simpleTree'
Perhaps you meant `siimpleTree' (line 6)
*Main> simpleTree
<interactive>:4:1:
Not in scope: `simpleTree'
Perhaps you meant `siimpleTree' (line 6)
*Main> :load sample.hs
[1 of 1] Compiling Main ( sample.hs, interpreted )
Ok, modules loaded: Main.
*Main> simpleTree
Node "parent" (Just (Node "left leaf" Nothing Nothing)) (Just (Node "right leaf" Nothing Nothing))
*Main> :type simpleTree
simpleTree :: Tree [Char]
*Main> Node Nothing
<interactive>:8:1:
No instance for (Show
(Maybe (Tree (Maybe a0))
-> Maybe (Tree (Maybe a0)) -> Tree (Maybe a0)))
arising from a use of `print'
Possible fix:
add an instance declaration for
(Show
(Maybe (Tree (Maybe a0))
-> Maybe (Tree (Maybe a0)) -> Tree (Maybe a0)))
In a stmt of an interactive GHCi command: print it
これだと、2つの子(子自身も二分木)を持つノード、1つの子ノード、あるいは子ノードを持たないノードは作成出来るけど、空のノードは作成できない事に気づく。
ということで書き換えてみる。
コード(BBEdit)
sample.hs
-- file: sample.hs
data Tree a = Node (Maybe (a, (Tree a), (Tree a)))
deriving (Show)
emptyTree = Node Nothing
simpleTree = Node (
Just ("parent",
(Node (Just ("left leaf", Node Nothing, Node Nothing))),
(Node (Just ("right leaf", Node Nothing, Node Nothing)))
))
入出力結果(Terminal)
$ ghci
GHCi, version 7.4.2: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :load sample.hs
[1 of 1] Compiling Main ( sample.hs, interpreted )
Ok, modules loaded: Main.
*Main> emptyTree
Node Nothing
*Main> :type it
it :: Tree a
*Main> simpleTree
Node (Just ("parent",Node (Just ("left leaf",Node Nothing,Node Nothing)),Node (Just ("right leaf",Node Nothing,Node Nothing))))
*Main> :type simpleTree
simpleTree :: Tree [Char]
*Main> empty = Node Nothing
<interactive>:7:7: parse error on input `='
*Main> let empty = Node Nothing
*Main> empty
Node Nothing
*Main> left = Node (Just ("left leaf", empty, empty))
<interactive>:10:6: parse error on input `='
*Main> let left = Node (Just ("left leaf", empty, empty))
*Main> left
Node (Just ("left leaf",Node Nothing,Node Nothing))
*Main> :type it
it :: Tree [Char]
*Main> let right = Node (Just ("right leaf", empty, empty))
*Main> :type right
right :: Tree [Char]
*Main> let parent = Node (Just("parent", left, right))
*Main> parent
Node (Just ("parent",Node (Just ("left leaf",Node Nothing,Node Nothing)),Node (Just ("right leaf",Node Nothing,Node Nothing))))
*Main> :type parent
parent :: Tree [Char]
*Main> :quit
Leaving GHCi.
$
これで空のノードも作成できるようになった。
0 コメント:
コメントを投稿