Created
March 10, 2011 16:21
-
-
Save jskripsky/864387 to your computer and use it in GitHub Desktop.
Sum of Squares in F#: imperative, pure functional, native
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
let sqr x = x * x | |
let sumOfSquaresI nums = | |
let mutable acc = 0.0 | |
for x in nums do | |
acc <- acc + sqr x | |
acc | |
let rec sumOfSquaresF nums = | |
match nums with | |
| [] -> 0.0 | |
| head::tail -> sqr head + sumOfSquaresF tail | |
let sumOfSquares nums = | |
nums | |
|> Seq.map sqr | |
|> Seq.sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment