TrueLogic 仕事日記 このページをアンテナに追加 RSSフィード

2010-02-13Real World Haskell 3章の練習問題のつづき

Real World Haskell 日本語版 p72

6. サブリストの長さでソートする

import Data.List
mySortSubList :: a? -> a? 
mySortSubList a = sortBy myCompareListLen a

myCompareListLen :: [a] -> [a] -> Ordering
myCompareListLen a b = compare (length a) (length b)

7. リストのリストを連結

myIntersperse :: Char? -> [Char]
myIntersperse  = 
myIntersperse (x:[]) = x
myIntersperse (x:xs) = x ++ "," ++ (myIntersperse xs)
myIntersperse ["foo","bar","baz","qeqrr"]

7.Treeの高さ

data Tree a = Node a (Tree a) (Tree a)
              | Empty
                deriving (Show)                        
myTreeHeight :: Tree a  -> Integer
myTreeHeight Empty = 0
myTreeHeight (Node a b c) =
                 1 + max (myTreeHeight b) (myTreeHeight c)
test data
x = Node 55 Empty Empty                 
x2 = Node 66 x x
x3 = Node 77 x Empty
x4 = Node 88 Empty x3 
トラックバック - http://specverification.g.hatena.ne.jp/n757uf/20100213