這裏中綴表達式的結果是樹的定義:data Tree = Leaf Char | Node (Char, Tree, Tree)
隨着算術表達式的解析樹如何生成在Haskell
我想要的形式寫一個函數treeToInfix
:
treeToInfix :: Tree -> String
下面是一些例子:
treeToInfix (Node ('*', (Node ('+', (Leaf 'a'), (Leaf 'b'))), (Leaf 'c')))
-- => "(a+b)*c"
treeToInfix (Node ('-', (Node ('+', (Leaf 'a') ,(Leaf 'b'))), (Leaf 'c')))
-- => "a+b-c"
treeToInfix (Node ('-', (Leaf 'c'), (Node ('+', (Leaf 'a') ,(Leaf 'b')))))
-- => "c-(a+b)"
treeToInfix (Node ('*', (Node ('/', (Leaf 'a'), (Leaf 'b'))), (Node ('/', (Leaf 'c'), (Leaf 'd')))))
-- => "a/b*c/d"
treeToInfix (Node ('+', (Node ('-', (Leaf 'a'), (Node ('*', (Leaf 'b'), (Leaf 'c'))))), (Node ('/', (Leaf 'd'), (Leaf 'e')))))
-- => "a-b*c+d/e"
我需要這個程序的算法幫助。
可能是作業問題? –