Created
July 20, 2015 17:24
-
-
Save richlander/5f40956d8ccffec8eb4d to your computer and use it in GitHub Desktop.
Simplified mutable values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// old approach - need a `ref` value | |
let sumSquares n = | |
let total = ref 0 | |
{ 1 .. n } |> Seq.iter (fun i -> | |
total := !total + i*i | |
) | |
!total | |
// new approach - `mutable` just works | |
let sumSquares' n = | |
let mutable total = 0 | |
{ 1 .. n } |> Seq.iter (fun i -> | |
total <- total + i*i | |
) | |
total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment