私は F# で単純な式パーサーを作成しています。各演算子について、特定の数のオペランドのみをサポートしたいと考えています (たとえば、Modulo の場合は 2 つ、If の場合は 3 つ)。ここに私が持っているものがあります:
type Operator =
| Modulo
| Equals
| If
let processOperator operands operator =
match operator with
| Modulo ->
match operands with
| [ a:string; b:string ] -> (Convert.ToInt32(a) % Convert.ToInt32(b)).ToString()
| _ -> failwith "wrong number of operands"
| Equals ->
match operands with
| [ a; b ] -> (a = b).ToString()
| _ -> failwith "wrong operands"
| If ->
match operands with
| [ a; b; c ] -> (if Convert.ToBoolean(a) then b else c).ToString()
| _ -> failwith "wrong operands"
内部リストの一致を取り除くか、単純化したいと思います。これを達成するための最良の方法は何ですか?複数のガードを使用する必要がありますか?