bonotakeの日記

ソフトウェア工学系研究者 → AIエンジニア → スクラムマスター・アジャイルコーチ

Seven trees 解答編:コード

もういいんじゃないでしょうかね。

ということで、解答です。
私のは、基本的にはku-ma-meさんのと同じですが、18ステップを一発変換に圧縮してます。で、以下の通り。結果的に9分岐になってます。
圧縮の仕方はまた後ほど。こちら
 

f :: Tree7 -> Tree
f (Leaf, Leaf, Leaf, Leaf, Leaf, Leaf, Leaf) = Leaf
f ((Node a Leaf), Leaf, Leaf, Leaf, Leaf, Leaf, Leaf) = (Node a Leaf)
f ((Node a (Node b c)), Leaf, Leaf, Leaf, Leaf, Leaf, Leaf) = (Node a (Node b (Node c Leaf)))
f (a, (Node b Leaf), Leaf, Leaf, Leaf, Leaf, Leaf) = (Node a (Node b Leaf))
f (a, (Node b (Node c d)), Leaf, Leaf, Leaf, Leaf, Leaf) = (Node a (Node b (Node c (Node d Leaf))))
f (a, b, (Node c d), Leaf, Leaf, Leaf, Leaf) = (Node a (Node b (Node c (Node d (Node Leaf Leaf)))))
f (a, b, c, (Node d e), Leaf, Leaf, Leaf) = (Node a (Node b (Node c (Node d (Node e (Node Leaf Leaf))))))
f (a, b, c, d, e, Leaf, Leaf) = (Node a (Node b (Node c (Node d (Node e Leaf)))))
f (a, b, c, d, e, f, g) = (Node a (Node b (Node c (Node d (Node e (Node f g))))))

g :: Tree -> Tree7
g Leaf = (Leaf, Leaf, Leaf, Leaf, Leaf, Leaf, Leaf)
g (Node a Leaf) = ((Node a Leaf), Leaf, Leaf, Leaf, Leaf, Leaf, Leaf)
g (Node a (Node b Leaf)) = (a, (Node b Leaf), Leaf, Leaf, Leaf, Leaf, Leaf)
g (Node a (Node b (Node c Leaf))) = ((Node a (Node b c)), Leaf, Leaf, Leaf, Leaf, Leaf, Leaf)
g (Node a (Node b (Node c (Node d Leaf)))) = (a, (Node b (Node c d)), Leaf, Leaf, Leaf, Leaf, Leaf)
g (Node a (Node b (Node c (Node d (Node Leaf Leaf))))) = (a, b, (Node c d), Leaf, Leaf, Leaf, Leaf)
g (Node a (Node b (Node c (Node d (Node e Leaf))))) = (a, b, c, d, e, Leaf, Leaf)
g (Node a (Node b (Node c (Node d (Node e (Node Leaf Leaf)))))) = (a, b, c, (Node d e), Leaf, Leaf, Leaf)
g (Node a (Node b (Node c (Node d (Node e (Node f g)))))) = (a, b, c, d, e, f, g)
注:bonotakeは、amazon.co.jpを宣伝しリンクすることによってサイトが紹介料を獲得できる手段を提供することを目的に設定されたアフィリエイト宣伝プログラムである、 Amazonアソシエイト・プログラムの参加者です。