2016-04-09 15 views
1

Desen eşleştirmelerini korumalarla yönetirken bir istisna atmanın alternatifi nedir?Desen eşleştirmelerini korumalarla yönetirken bir istisna atmanın alternatifi nedir?

[<Literal>] 
let Objective = 33 

let (|NotGame|IsGame|) p = 
    match p with 
    | LessThanGame v when v < Objective -> NotGame v 
    | Game   v when v >= Objective -> IsGame v 

Uyarı:

Eksik kalıbını derleyici herhangi bir şikayet istemiyorum bu ifadenin

üzerinde eşleşir.

let (|NotGame|IsGame|) p = 
    match p with 
    | LessThanGame v when v < Objective -> NotGame v 
    | Game   v when v >= Objective -> IsGame v 
    | _ v        -> IsGame v 

tüm kodudur:

let (|NotGame|IsGame|) p = 
    match p with 
    | LessThanGame v when v < Objective -> NotGame v 
    | Game   v when v >= Objective -> IsGame v 
    | _ -> failwith "idk..." 

Ne Doğrusu yapacağını şudur:

Sonuç olarak

, ne yapmam istemiyorum şudur burada:

// *********** 
// Game of 33 
// ************************************************************ 
(*Types*) 
// ************************************************************ 
type Player = 
    | Player1 
    | Player2 

type Shot = 
    | TwoPointer 
    | ThreePointer 
    | FoulShot 
    | TwoFoulShots 
    | ThreeFoulShots 

type PlayerScore = {Player:Player; Points:Points} 

and Points = 
    | LessThanGame of int 
    | Game of int 

type Posession = Posession of Player 

[<Literal>] 
let Objective = 33 

// ************************************************************ 
(*Active Patterns*) 
// ************************************************************ 
let (|NotGame|IsGame|) = function 
    | LessThanGame v when v < Objective -> NotGame v 
    | Game   v when v >= Objective -> IsGame v 
    | _ -> failwith "idk..." 

// ************************************************************ 
(*Functions*) 
// ************************************************************ 
let makeShot shot (shooter, defender) = 

    let ballHandler = shooter.Player 
    let points = shooter.Points 

    let shotValue = match shot with 
        | TwoPointer | TwoFoulShots -> 2 
        | ThreePointer | ThreeFoulShots -> 3 
        | FoulShot      -> 1 

    match points with 
    | NotGame p -> if p + shotValue < Objective then 
         { Player=ballHandler; Points=LessThanGame (p + shotValue) }, defender 
        else { Player=ballHandler; Points=Game   (p + shotValue) }, defender 

    | IsGame p -> { Player=ballHandler; Points=Game p }, defender 

let startGame = 

    let player1Score = { Player=Player1; Points=LessThanGame 0 } 
    let player2Score = { Player=Player2; Points=LessThanGame 0 } 

    (player1Score, player2Score) 

// ************************************************************ 
(*Client*) 
// ************************************************************ 
let player1Score, player2Score = startGame 

let player1, player2 = (player1Score, player2Score) |> makeShot TwoPointer 
let shooter, defender = player2, player1 

let final = (shooter, defender) |> makeShot ThreePointer 
           |> makeShot FoulShot 

           |> makeShot ThreePointer 
           |> makeShot TwoFoulShots 

           |> makeShot ThreePointer 
           |> makeShot ThreeFoulShots 

           |> makeShot ThreePointer 
           |> makeShot ThreePointer 
           |> makeShot ThreePointer 
           |> makeShot ThreePointer 
           |> makeShot ThreePointer 

Çözünürlük: Guy Coder tavsiye olarak

, ben alternatif olarak/else if koşulları kullanabilirsiniz. Böylece, bu benim için bir istisna atma ihtiyacını ortadan kaldırdı.

// ************************************************************ 
(*Types*) 
// ************************************************************ 
type Player = | Player1 | Player2 

type Shot = 
    | TwoPointer| ThreePointer 
    | FoulShot | TwoFoulShots | ThreeFoulShots 

type PlayerScore = {Player:Player; Game:Game} 

and Game = 
    | Underway of int 
    | AlmostGame of int 
    | GameTime of int 

[<Literal>] 
let Objective = 33 

[<Literal>] 
let MaxFoulShots = 3 

let (|Underway|AlmostGame|Game|) (score,shot) = 

    let shotValue = match shot with 
        | FoulShot      -> 1 
        | TwoPointer | TwoFoulShots -> 2 
        | ThreePointer | ThreeFoulShots -> 3 

    match score, shotValue with 
    | Underway s,v -> if (s + v) <= (Objective - MaxFoulShots) 
         then Underway (s + v) 
         else AlmostGame (s + v) 

    | AlmostGame s,v -> if ((s+v) < Objective) 
         then AlmostGame (s + v) 
         else Game Objective 

    | GameTime s,v -> Game s 

// ************************************************************ 
(*Functions*) 
// ************************************************************ 
let makeShot shot (shooter, defender) = 

    match (shooter.Game, shot) with 
    | Underway p -> { shooter with Game=Underway p }, defender 
    | AlmostGame p -> { shooter with Game=AlmostGame p }, defender 
    | Game p  -> { shooter with Game=GameTime p }, defender 

let startGame = 

    let player1Score = { Player=Player1; Game=Underway 0 } 
    let player2Score = { Player=Player2; Game=Underway 0 } 

    (player1Score, player2Score) 

// ************************************************************ 
(*Client*) 
// ************************************************************ 
let player1Score, player2Score = startGame 

let player1, player2 = (player1Score, player2Score) |> makeShot TwoPointer 
let shooter, defender = player2, player1 

let final = (shooter, defender) |> makeShot ThreePointer 
           |> makeShot FoulShot 

           |> makeShot ThreePointer 
           |> makeShot TwoFoulShots 

           |> makeShot ThreePointer 
           |> makeShot ThreeFoulShots 

           |> makeShot ThreePointer 
           |> makeShot ThreePointer 
           |> makeShot ThreePointer 
           |> makeShot ThreePointer 
           |> makeShot ThreePointer 

           |> makeShot FoulShot 
           |> makeShot FoulShot 
           |> makeShot ThreeFoulShots 
+1

derleyici kontrol durur ve bir joker yoksa bir uyarı verir ne zaman bir var olur olmaz. Gerçekten düzeltilemez ve sanırım dün –

+0

John bir dupe oldu, fark bir sendika davasıyla ilgili bir değere sahip olduğum. Dünkü soruda ek veriler yoktu. Bunun yerine değer sadece bir int ve sendika davası değildi. –

+1

Oh _ _ _ _ _ _ _ _ _ bir _ey de yap 1lamaz. Yinelenen bir –

cevap

1

Doğru sadece iki kova içine girdi sınıflandırmak için çalışıyoruz sorununuzu anlamak ve koşullu less than ise.

böylece daha sonra desen eşleştirme bırakmak ve sadece bir if deyimi kullanın. Fonksiyonel olduğunu söyleyen hiçbir kural, tüm kararlar için bir model eşleştirmesi kullanmalıdır.