我手头有以下代码:
match intersection with
| None ->
printfn "Please provide an empty intersection for ring placement"
gameState
| Some x ->
match x.Status with
| Empty ->
let piece = { Color = gameState.Active.Color; Type = Ring }
putPieceOnIntersection gameState.Board pos piece
printfn "%s ring placed at %A" (colorStr gameState.Active.Color) pos
// Decide if we ended this phase
let updatedPhase = if ringsPlaced = 10 then Main else Start(ringsPlaced + 1)
let newActivePlayer = gameState.Players |> Array.find (fun p -> p.Color = invertColor gameState.Active.Color)
let updatedGameState = { gameState with Active = newActivePlayer; CurrentPhase = updatedPhase }
updatedGameState
| _ ->
printfn "Please provide an empty intersection for ring placement"
gameState
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,如果变量交集是None或其Status不同于empty,我应该完全相同的分支打印一些文本并返回.但是我不知道如何在F#中做那种条件表达式,这样我就可以共享同一个分支.在命令式编程中我会很容易地做到这一点,但在F#中我该怎么做?
谢谢
Tar*_*mil 12
如果Status是记录字段,那么您可以这样做:
match intersection with
| Some { Status = Empty } ->
// Code for empty...
| _ ->
printfn "Please provide an empty intersection for ring placement"
gameState
Run Code Online (Sandbox Code Playgroud)
否则,您可以使用警卫:
match intersection with
| Some x when x.Status = Empty ->
// Code for empty...
| _ ->
printfn "Please provide an empty intersection for ring placement"
gameState
Run Code Online (Sandbox Code Playgroud)