下面的簡化函數試圖簡化類型
((2 + 3) * (2 + 4))
的數學表達式來
(2 * (3 + 4))
。如何使用有源圖案來實現的數學表達式簡化
理想情況下,我想寫出匹配表達式爲:
| Product (Sum (c, a), Sum (c, b)) -> Product (c, Sum (a, b))
但是,這給我的「C在這種模式勢必兩次」錯誤。所以我訴諸守衛條件。
我想知道是否有更好的方法來使用主動模式來實現這一點?
type Expr =
| Number of int
| Sum of Expr * Expr
| Product of Expr * Expr
let rec eval =
function
| Number n -> n
| Sum (l, r) -> eval l + eval r
| Product (l, r) -> eval l * eval r
let rec show =
function
| Number n -> n.ToString()
| Sum (l, r) -> "(" + show l + " + " + show r + ")"
| Product (l, r) -> "(" + show l + " * " + show r + ")"
let rec simplify =
function
| Product (Sum (c, a), Sum (d, b)) when (c = d) -> Product (c, Sum (a, b))
| Sum (l, r) -> Sum (simplify l, simplify r)
| Product (l, r) -> Product (simplify l, simplify r)
| e -> e
let c = Product (Sum (Number 2, Number 3), Sum (Number 2, Number 4))
show c
eval c
show (simplify c)
您擁有它的方式是正確的,沒有簡短的符號。 –
你如何「簡化」(2 + 3)*(2 + 4)'等於'30'到'2 *(3 + 4)'等於'14'?這些東西並不等同。 – TheInnerLight
[「Expert F#4.0」](http://www.worldcat.org/oclc/936182944)有關於[簡化代數表達式]的章節(https://books.google.com/books?id=XKhPCwAAQBAJ&pg=PA339&lpg = PA339&DQ =專家+ F%23 + 4.0 +簡化+代數+表達。&源= BL&OTS = GHXjjeHPbV&SIG = I4wbdiF6WVWOx4EdES6KS4ohh7M&HL = EN&SA = X&VED = 0ahUKEwio54a0jKHSAhXC1CYKHQemCd4Q6AEIITAB#v = onepage&q =專家%20F%23%204.0%20Simplifying%20Algebraic%20Expressions。&F = false)這可能有幫助。 –