【发布时间】:2020-05-19 20:13:03
【问题描述】:
我对 Haskell 还很陌生。我想做的是交换树中孩子的顺序。如果它们都是整数,我不确定如何让我的代码交换两个孩子。谢谢!
# For example:
# If t1 = (* (+ 20 1) (- 10 8))
# Then when I call 'swap t1' I should get (* (- 8 10) (+ 1 20)) but I get (* (- 10 8) (+ 20 1))
#Here is my code:
data Tree =
TInt Integer
| TOp String Tree Tree
t1 = TOp "*" (TOp "+" (TInt 20) (TInt 1))
(TOp "-" (TInt 10) (TInt 8))
swap :: Tree -> Tree
swap (TInt i) = TInt i
swap (TOp s first second) = TOp s second first
【问题讨论】: