我正在努力為我的上一個任務尋找解決方案。我必須實作一個函式sumSucc,它接受一個IntTree并回傳從succTree.
succTree 基本上回傳給定樹的后繼,定義如下:
succTree :: IntTree -> IntTree
succTree t = tmap ( 1) t
我知道我可以通過執行以下操作輕松解決此問題:
sumSucc :: IntTree -> Int
sumSucc (Leaf x) = x 1
sumSucc (Branch (x, xl, xr)) = (x 1) sumSucc xl sumSucc xr
但我不得不使用函式succTree內sumSucc。提前致謝。
uj5u.com熱心網友回復:
你可以實作另一個函式sumTree,然后sumSucc相當于:
sumSucc :: IntTree -> Int
sumSucc x = sumTree (succTree x)
sumTree :: IntTree -> Int
sumTree (Leaf x) = …
sumTree (Branch (x, xl, xr)) = …
在這里,你這樣應該實作…的部分sumTree。我把它留作練習。
該sumSucc可以不帶引數來實作,并利用(.) :: (b -> c) -> (a -> b) -> a -> c:
sumSucc :: IntTree -> Int
sumSucc = sumTree . succTree
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/337434.html
下一篇:如何實作自然數的集合論定義>
