2014年1月12日日曜日

開発環境

Real World Haskell―実戦で学ぶ関数型言語プログラミング(Bryan O'Sullivan (著)、 John Goerzen (著)、 Don Stewart (著)、山下 伸夫 (翻訳)、伊東 勝利 (翻訳)、株式会社タイムインターメディア (翻訳)、オライリージャパン)の3章(型を定義し、関数を単純化する)、3.7(再帰型)、練習問題2.を解いてみる。

その他参考書籍

練習問題2.

コード(BBEdit)

Sample.hs

module Main where

data Tree a = Tree a
                   (Maybe (Tree a))
                   (Maybe (Tree a))
              deriving (Show)

simpleTree :: Tree String
simpleTree = Tree "parent" (Just (Tree "left child"
                                       Nothing
                                       Nothing))
                           (Just (Tree "right child"
                                        Nothing
                                        Nothing))

leftTree :: Tree String
leftTree = Tree "parent" (Just (Tree "left child"
                                     Nothing
                                     Nothing))
                         Nothing

rightTree :: Tree String
rightTree = Tree "parent" Nothing
                          (Just (Tree "right child"
                                      Nothing
                                      Nothing))

emptyTree :: Tree (Maybe a)
emptyTree = Tree Nothing Nothing Nothing

入出力結果(Terminal, インタプリタghci)

$ ghci
GHCi, version 7.6.3: 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
[1 of 1] Compiling Main             ( Sample.hs, interpreted )
Ok, modules loaded: Main.
*Main> simpleTree
Tree "parent" (Just (Tree "left child" Nothing Nothing)) (Just (Tree "right child" Nothing Nothing))
*Main> leftTree
Tree "parent" (Just (Tree "left child" Nothing Nothing)) Nothing
*Main> rightTree
Tree "parent" Nothing (Just (Tree "right child" Nothing Nothing))
*Main> emptyTree
Tree Nothing Nothing Nothing
*Main> :quit 
Leaving GHCi.
$

0 コメント:

コメントを投稿