/ F#

HR F#: Sum of Odd Elements

The problem Sum of Odd Elements:

You are given a list. Return the sum of odd elements from the given list.

The solusion is basically just an invocation of the List.fold from the standard library:

let f arr =
  arr
  |> List.fold (fun s v -> if Math.Abs(v % 2) = 1 then s + v else s) 0

Math.Abs is required because for negative numbers % 2 returns either 0 or -1.

Alex Netkachov

Alex Netkachov

Alex likes functional programming and algorithms. Apart from programming, his favourites are walking with his family in the parks and national trails and reading about universe and history.

Read More