2016-08-30 4 views
5

多分珍しい質問がありますが、どのようにはF#の関数とパターンマッチングを使って一致しますか?パターンはF#の関数に一致します

は、次のことを想像してみて:
ように私には、複数回使用される複数の関数シグネチャを、持っている:

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

今自体が機能fをとる関数evaluateを、想像してみてください。 fの署名は、である必要があります。 このようなケースにはどのように一致しますか?型パターンマッチングとデリゲートを使用:

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:デリゲートと組合を使用
試験1番:


は、私は、次のことを試みた

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" 


これらの例の問題は、...の代理人が実際の関数の代わりにマッチすることです。私はこのようなsomethinkを見たい :

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#は関数パターンマッチングのための特別な構文を持っていますか?
そうでない場合は、どうしてですか?私が何かを逃しているのですか、それともの機能がなので、関数を他のものと同じように一致させることも重要ではありませんか?代わりに、デリゲートを使用しての

+4

なぜ関数の代わりに代理人を使用する必要がありますか? –

+0

@FyodorSoikin:これまでに試したことがありますが、タイプミス(その時は分かりませんでした)のためにタイプを関数として定義できませんでした。私はそれが不可能だと思った:/ – Unknown6656

答えて

10

、単に直接の機能を使用して作業を定義します。

> 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" 
:あなたはこれをやった後は、必要に応じて

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出力を示し、以下のように)それらを呼び出すことができます

+1

**私はあなたを愛しています、サー!**どういうわけか、これは以前私のために働いていませんでした。私はそれができないと思ったので... – Unknown6656

関連する問題