我是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}
我理解像總結了樹等的葉子的簡單的例子,但不能想出了一個解決這個問題。我真的很感激一個小提示。謝謝你們
這甚至是什麼_supposed_做什麼?在樹中有多個整數值,但在MyRecord中只有一個,你需要指定一些如何組合它們的想法。 – leftaroundabout