Skip to content

Instantly share code, notes, and snippets.

@jskripsky
Created March 10, 2011 16:21
Show Gist options
  • Save jskripsky/864387 to your computer and use it in GitHub Desktop.
Save jskripsky/864387 to your computer and use it in GitHub Desktop.
Sum of Squares in F#: imperative, pure functional, native
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