1

我正在用 F# 编写一个简单的表达式解析器,对于每个运算符,我只想支持一定数量的操作数(例如,两个用于 Modulo,三个用于 If)。这是我所拥有的:

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"

我想摆脱或简化内部列表匹配。实现这一目标的最佳方法是什么?我应该使用多个警卫吗?

4

2 回答 2

4
open System

type Operator =
    | Modulo
    | Equals
    | If

let processOperator operands operator =
    match (operator, operands) with
    | Modulo, [a: string; b] -> string ((int a) % (int b))
    | Equals, [a; b] -> string (a = b)
    | If, [a; b; c]  -> if Convert.ToBoolean(a) then b else c
    | _ -> failwith "wrong number of operands"

但是我建议将操作数的这个逻辑移到解析器中,这样你就可以得到一个干净的运算符表达式,它更符合地道且易于处理,最后你会得到这样的东西:

open System

type Operator =
    | Modulo of int * int
    | Equals of int * int
    | If of bool * string * string

let processOperator = function
    | Modulo (a, b) -> string (a % b)
    | Equals (a, b) -> string (a = b)
    | If (a, b, c)  -> if a then b else c
于 2014-03-16T19:03:47.363 回答
3

折叠匹配的操作数:

let processOperator operands operator =
    match operator, operands with
    | Modulo, [a; b] -> (Convert.ToInt32(a) % Convert.ToInt32(b)).ToString()
    | Equals, [a; b] -> (a = b).ToString()
    | If, [ a; b; c ] -> (if Convert.ToBoolean(a) then b else c).ToString()
    | _ -> failwith "wrong number of operands"

更好的是,如果可以的话,将数据类型更改为以下内容。

type Operator =
    | Modulo of string * string
    | Equals of string * string
    | If of string * string * string

那么在比赛中,你就不能再失败了。

于 2014-03-16T18:57:25.053 回答