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

后端 未结 8 522
-上瘾入骨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:16

    I do not know about F# very well, but F# is a functional language. Usually, there is no such thing as "for" or "while" loops in functional programming languages.

    Functional languages define functions in a mathematical sense (like f(x) => ...). Writing a program comes down to defining and combining a set of mathematical functions. This means that the only way of coding loops is using recursion.

    In Mathematics, there is no way of saying:

    f(x) => "do 5 times this"
    

    What you'd do is define f like:

                     count > 0  : f(x, count-1)
    f(x, count) => {
                     count <= 0 : ...
    

    And then use this function as in:

    y = f(x, 5)
    

    This would be exactly how you implement functions in functional languages. At least, this is true for purely functional languages like Haskell...

提交回复
热议问题