【问题标题】:Labeling a tree depth-first in-order in haskell在haskell中按顺序标记树深度优先
【发布时间】: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 &lt;- get 是做什么的?走哪条线有关系吗?
  • 谢谢你,这就是我所需要的:)

标签: haskell tree label


【解决方案1】:

我刚刚回答了较早的问题here。很抱歉错误地归因于你。关键是,当你编写你的类型时,你还告诉了 Haskell 如何traverse 你的数据结构,以确保get 发生在中间。

你自己在本地写错的程序是你通过定义类型就已经正确编写的程序,如果你知道的话。

【讨论】:

  • 只是为了避免混淆,这不是我的问题,我只是有同样的问题。阅读您的解决方案非常有趣,并且非常优雅地使用了一些 haskell 的属性。
猜你喜欢
  • 2014-12-11
  • 1970-01-01
  • 2012-11-20
  • 1970-01-01
  • 1970-01-01
  • 2012-12-12
  • 2012-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多