在F#中以无点样式进行匹配?

sdg*_*sdh 1 syntax f# pattern-matching pointfree

考虑以下代码:

type Fruit = Apple | Banana

let totalCost fruits = 
  fruits
  |> Seq.map (fun fruit -> 
    match fruit with
    | Apple -> 0.50
    | Banana -> 0.70
  )
  |> Seq.sum
Run Code Online (Sandbox Code Playgroud)

totalCost可以用fruit删除标识符的方式重写得更简洁吗?

像这样:

// Not real code
let totalCost fruits = 
  fruits
  |> Seq.map (
    match
    | Apple -> 0.50
    | Banana -> 0.70
  )
  |> Seq.sum
Run Code Online (Sandbox Code Playgroud)

Fyo*_*kin 7

您要寻找的关键字是function

|> Seq.map ( 
    function
    | Apple -> 0.50 
    | Banana -> 0.70
)
Run Code Online (Sandbox Code Playgroud)

function 屈服于 fun x -> match x with