/ 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 =
  |> 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, algorithms and code reviews. Apart from programming, his favourites are walking with his family in the parks and national trails and reading books.

Read More

Why not to stay updated if the subject is interesting? Join Telegram channel Alex@Net or follow alex_at_net on Twitter. Or just, use the comments form below.