【发布时间】:2015-10-17 19:49:08
【问题描述】:
我基本上和这里问的问题相同:Haskell label a binary tree through depht-first in-order traversel 但实际上从未给出答案,由于缺乏声誉,我无法在那里发表评论。
现在我有一个函数label:
label :: MonadState m Int => Tree a -> m (Tree (Int, a))
label Leaf = return Leaf
label (Branch leftTree a rightTree) = do n <- get
modify (+1)
l' <- label leftTree
r' <- label rightTree
return (Branch l' (n,a) r')
Tree a = Leaf | Branch (Tree a) a (Tree a).
现在这标记树的广度优先。现在我想首先标记leftTree,然后标记Branch 本身,然后标记rightTree,但我不知道如何实现这一点,并且其他线程对我没有进一步的帮助。
【问题讨论】:
-
提示:
n <- get是做什么的?走哪条线有关系吗? -
谢谢你,这就是我所需要的:)