模式匹配F#中的函数

时间:2016-08-30 17:33:18

标签: function f# pattern-matching

我有一个可能不寻常的问题,但如何使用模式匹配匹配F#中的函数?

想象一下:
我有多个功能签名,将多次使用,如:

binary function:  int -> int -> int
unary function:   int -> int
boolean function: int -> int -> bool
...

现在假设函数evaluate,它本身采用函数ff 的签名必须是上面列出的一个。 我如何匹配这种情况?

我尝试了以下事项:
测试1号:使用代表和工会:

type UnaryFunction = delegate of int -> int
type BinaryFunction = delegate of (int -> int) -> int
type BooleanFunction = delegate of (int -> int) -> bool

type Functions =
    | Unary of UnaryFunction
    | Binary of BinaryFunction
    | Boolean of BooleanFunction

// ...

let evaluate f = // signature: Functions -> string
    match f with
    | Unary u ->
        let test_result = u.Invoke 3
        sprintf "the result of the unary function is %d" test_result
    | Binary b ->
        let test_result = b.Invoke 315 42
        sprintf "the result of the binary function is %d" test_result
    | Boolean o ->
        let test_result = o.Invoke 315 42
        if test_result then "yeah" else "nope"

测试2号:使用类型模式匹配和委托:

type UnaryFunction = delegate of int -> int
type BinaryFunction = delegate of (int -> int) -> int
type BooleanFunction = delegate of (int -> int) -> bool

let evaluate f =
    match f with
    | ?: UnaryFunction as u ->
        let test_result = u.Invoke 3
        sprintf "the result of the unary function is %d" test_result
    | ?: BinaryFunction as b ->
        let test_result = b.Invoke 315 42
        sprintf "the result of the binary function is %d" test_result
    | ?: BooleanFunction as o ->
        let test_result = o.Invoke 315 42
        if test_result then "yeah" else "nope"
    | _ -> "invalid function type"

<小时/> 这些示例的问题是, ... 的委托将匹配而不是实际的功能。 我想看到这样的想法:

let evaluate f =
    match f with
    | ?: (int -> int) as u ->
        let test_result = u 3
        sprintf "the result of the unary function is %d" test_result
    | ?: ((int -> int) -> int) as b ->
        let test_result = b 315 42
        sprintf "the result of the binary function is %d" test_result
    | ?: ((int -> int) -> bool) as o ->
        let test_result = o 315 42
        if test_result then "yeah" else "nope"
    | _ -> "invalid function type"

F#是否具有函数模式匹配的特殊语法?
如果没有,为什么呢?我是否遗漏了某些东西,或者能否像其他任何东西一样匹配函数也很重要,因为这是功能性语言?

1 个答案:

答案 0 :(得分:10)

不是使用委托,而是直接使用函数定义工作:

type UnaryFunction = int -> int
type BinaryFunction = int -> int -> int
type BooleanFunction = int -> int -> bool

type Functions =
    | Unary of UnaryFunction    
    | Binary of BinaryFunction
    | Boolean of BooleanFunction

// ...

let evaluate f = // signature: Functions -> string
    match f with
    | Unary u ->
        let test_result = u 3
        sprintf "the result of the unary function is %d" test_result
    | Binary b ->
        let test_result = b 315 42
        sprintf "the result of the binary function is %d" test_result
    | Boolean o ->
        let test_result = o 315 42
        if test_result then "yeah" else "nope"

完成此操作后,您可以根据需要调用它们(如下所示,显示FSI输出):

> evaluate (Unary (fun x -> x + 3));;
val it : string = "the result of the unary function is 6"

> let someBinaryFunction x y = x * y;;
val someBinaryFunction : x:int -> y:int -> int

> Binary someBinaryFunction |> evaluate;;
val it : string = "the result of the binary function is 13230"