頂点間の接続のリストを指定して、指定された頂点を持つツリーを構築するために、以下のコードを記述しました。
type Connection = (Int,Int)
data Tree = Leaf Int | Node Int [Tree] deriving (Eq,Read,Show)
makeTree :: Int -> [Connection] -> Tree
makeTree x [] = Leaf x
makeTree indx connections = Node indx otherTrees where
otherTrees = [makeTree i cx | i <- directConnections, let cx = removeConnectionsTo indx connections]
directConnections = map (\(x,y) -> if (x == indx) then y else x) $ filter (\(x,y) -> x == indx || y == indx) connections
removeConnectionsTo :: Int -> [Connection] -> [Connection]
removeConnectionsTo indx = filter (\(x,y) -> x /= indx && y /= indx)
何らかの理由で、以下の入力は驚くほど異なる結果をもたらします。
makeTree 1 [(1,2),(1,3)]
私にくれますNode 1 [Leaf 2,Leaf 3]
makeTree 1 [(1,2),(1,5),(2,3),(2,4),(5,6),(5,7)]
私にくれますNode 1 [Node 2 [Node 3 [],Node 4 []],Node 5 [Node 6 [],Node 7 []]]
OSX10.8.2でGHCiバージョン7.4.1を実行しています。
最初の例(正しい)でLeafを2回取得する理由はわかりませんが、2番目の例(正しくない)ではサブツリーリストが空のノードです。