F# 中是否有类似于 Switch Case 命令的内容?

Is there anything similar to a Switch Case command in F#?

我是第一次练习 F# 编程,我想知道该语言中是否有一个命令可以模拟 C# 中的 Switch/Case 命令的等效命令?我想确保优化程序,这样它们就不必每次都重新设置以尝试不同的程序路径 (例如,创建三种面积计算器并不得不重新设置尝试每一个。)

编辑:我完全忘记了显示代码作为示例。 这就是我想尝试编辑的内容。

```module AreaCalculator =
printfn "------------------------------"
printfn "Enter 1 to find a rectangle's area."
printfn "Enter 2 to find a circle's area."
printfn "Enter 3 to find a triangle's area."
printfn "Enter here: "
let stringInput = System.Console.ReadLine()

if stringInput = "1" then
  printfn "What is the rectangle's length: "
  let rlString = System.Console.ReadLine()
  printfn "What is the rectangle's width: "
  let rwString = System.Console.ReadLine()

  let rlInt = rlString |> int
  let rwInt = rwString |> int
  let rectArea = rlInt * rwInt

  printfn "The area of the rectangle is: %i" rectArea
elif stringInput = "2" then
  let PI = 3.14156
  printfn "What is the circle's radius: "
  let radiusString = System.Console.ReadLine()
  let radiusInt = radiusString |> float
  let cirlceArea = (radiusInt * radiusInt) * PI

  printfn "The area of the circle is : %f" cirlceArea


elif stringInput = "3" then
  printfn "What is the triangle's base: "
  let baseString = System.Console.ReadLine()
  printfn "What is the triangle's height: "
  let heightString = System.Console.ReadLine()
  
  let baseInt = baseString |> int
  let heightInt = heightString |> int
  let triArea = (heightInt * baseInt)/2

  printfn "The area of the triangle is: %i" triArea

else
  printfn "Please try again"

它按原样运行良好,但我想看看我是否可以重新编写程序,这样就不必在每次计算不同形状的面积时都重新设置。我试过使 stringInput 成为一个可变变量,正如我在演示文稿中看到的那样,但它只会导致这样的错误:

/home/runner/F-Practice-1/main.fs(59,5):错误 FS0588:此 'let' 之后的块未完成。每个代码块都是一个表达式,并且必须有一个结果。 'let' 不能是块中的最后一个代码元素。考虑给这个块一个明确的结果。

我该怎么做才能解决这个问题?

F# 中的等效构造称为 match:

let stringInput = System.Console.ReadLine()
match stringInput with
    | "1" ->
        printfn "What is the rectangle's length: "
        // ...
    | "2" ->
        printfn "What is the circle's radius: "
        // ...
    | "3" ->
        printfn "What is the triangle's base: "
        // ...
    | _ ->
        printfn "Please try again"

更多详情here