[英]F# Discriminated Unions and Printing
解決此問題的方法是通過expr
上的模式匹配實現toString
,並為Const
和BinOpr
輸出相應的字符串:
Const
,您只需將int
轉換為字符串即可。 BinOpr
,您必須構建一個表單的字符串(<expr> <op> <expr>)
。 嘗試自己實現這個功能,並不是那么困難但是如果你遇到困難,我提供下面的解決方案。
let rec toString expr =
match expr with
| Const x -> string x
| BinOpr (e1, op, e2) -> sprintf "(%s %s %s)" (toString e1) op (toString e2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.