Why “do…while” does not exist in F#

后端 未结 8 510
-上瘾入骨i
-上瘾入骨i 2021-02-04 00:36

I cannot find \"do...while...\"

I have to code like this:

let bubbleSort a=
    let n = Array.length a
    let mutable swapped = true
    let mutable i =         


        
8条回答
  •  星月不相逢
    2021-02-04 01:27

    do/while is not available because F# is a functional language and this kind of construct is specific to imperative languages.

    break/continue is also not available for the same reasons.

    However, you can still write do/while in F#. The following code blocks are equivalent :

    in C#

    do
    {
        System.Console.WriteLine("processing something...");
        System.Console.WriteLine("doing something complicated");
    
        System.Console.Write("continue?");
    } while (Console.ReadLine() == "y");
    

    in F#

    let doSomethingAndContinue() =
      printfn "processing something..."
      printfn "doing something complicated"
      printf  "continue?"
      System.Console.ReadLine()="y"
    
    while doSomethingAndContinue() do ignore None
    

提交回复
热议问题