簡體   English   中英

模式匹配F#中的函數

[英]Pattern match a function in F#

我有一個可能不尋常的問題,但是如何使用模式匹配匹配F#中的函數?

想象一下:
我有多個功能簽名,將多次使用,如:

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

現在想象一下函數evaluate ,它本身就是一個函數f f的簽名必須是上面列出的一個。 我如何匹配這種情況?


我試過以下的事情:
測試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#是否有特殊的函數模式匹配語法?
如果沒有,為什么呢? 我是否遺漏了某些東西,或者是否能夠像其他任何東西一樣匹配函數也很重要,因為這是一種函數式語言?

而不是使用委托,只需直接使用函數定義工作:

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM