だから私は次のように定義されたツリーを持っています
data Tree a = Leaf | Node a (Tree a) (Tree a) deriving Show
Leaf を Leaf a と定義できることはわかっています。しかし、私は自分のノードに値を持たせたいだけです。私の問題は、検索を行うと、型の戻り値関数があることです
Tree a -> a
葉には価値がないので、葉に遭遇した場合は何もしないと言う方法がわかりません。試してみましnil
た , " "
,何もうまくいかないようです' '
。[]
コードを編集
data Tree a = Leaf | Node a (Tree a) (Tree a) deriving Show
breadthFirst :: Tree a -> [a]
breadthFirst x = _breadthFirst [x]
_breadthFirst :: [Tree a] -> [a]
_breadthFirst [] = []
_breadthFirst xs = map treeValue xs ++
_breadthFirst (concat (map immediateChildren xs))
immediateChildren :: Tree a -> [Tree a]
immediateChildren (Leaf) = []
immediateChildren (Node n left right) = [left, right]
treeValue :: Tree a -> a
treeValue (Leaf) = //this is where i need nil
treeValue (Node n left right) = n
test = breadthFirst (Node 1 (Node 2 (Node 4 Leaf Leaf) Leaf) (Node 3 Leaf (Node 5 Leaf Leaf)))
main =
do putStrLn $ show $ test