【发布时间】:2014-06-29 14:48:30
【问题描述】:
我是 Haskell 的新手,想将自定义树的叶子的值放入记录中。我从这个开始。
data MyTree = A Int | B Int MyTree | C Double | D Double MyTree
test = B 1 ( B 1( D 0.02(A 2)))
data MyRecord = MyRecord {A, B :: Int, C :: Double, D :: (Int,Double)}
emptyRecord = MyRecord{a = 0, b = 0, c = 0, d =(0,0)}
现在我是这样开始的:
MyTree2MyRecord :: MyTree -> MyRecord
MyTree2MyRecord(A a1) = emptyRecord{a = a1}
MyTree2MyRecord(B b1 myTree) = emptyRecord {b = b1}
MyTree2MyRecord(C c1) = emptyRecord {c = c1}
MyTree2MyRecord(D d1 myTree) = emptyRecord {d = d1}
where mytree = MyTree2MyRecord -{dont know the recursive call to iterate through the tree and get the values of the leafs}
我理解简单的例子,比如总结一棵树的叶子等,但无法找到解决这个问题的方法。我真的很感激一个小提示。谢谢大家
【问题讨论】:
-
它甚至应该做什么?一棵树中有多个整数值,但
MyRecord中只有一个,您需要指定如何组合它们的一些想法。
标签: haskell recursion tree record