Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Haskell

Idiom #202 Sum of squares

Calculate the sum of squares s of data, an array of floating point values.

sumOfSquares = sum . map (^2)

Because data is a keyword in Haskell, this is a function for computing the sum of squares.
sumOfSquares :: Num c => [c] -> c
(defn square [x] (* x x))

(def s (transduce (map square) + data))

This doesn't create any intermediate collection

New implementation...
< >
Bart