How do you return multiple values and assign them to mutable variables?

后端 未结 3 1318
谎友^
谎友^ 2021-02-20 17:42

This is what I have so far.

let Swap (left : int , right : int ) = (right, left)

let mutable x = 5
let mutable y = 10

let (newX, newY) = Swap(x, y) //<--thi         


        
3条回答
  •  忘掉有多难
    2021-02-20 18:12

    The code you have commented doesn't work because when you write "x, y" you create a new tuple that is an immutable value, so can't be updated. You could create a mutable tuple and then overwrite it with the result of the swap function if you want:

    let mutable toto = 5, 10 
    
    let swap (x, y) = y, x
    
    toto  <- swap toto
    

    My advice would be to investigate the immutable side of F#, look at the ways you can use immutable structures to achieve what you previously would have done using mutable values.

    Rob

提交回复
热议问题