# Project Euler Problem 1 - Functional approach?

I'm trying to learn F# right now coming from C# and I'm finding it a great difficulty to "reconfigure" my mind to the functional programming mindset. So I'm going to attempt a few Project Euler problems to learn how to code functionally.

Here's my attempt at problem 1, now I'm wondering prorimarily, is this an idiomatic approach?

// Project Euler - Problem 1
// If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
// Find the sum of all the multiples of 3 or 5 below 1000.
#light

open System

let multiples = [for n in 1..1000 do if n % 3 = 0 || n % 5 = 0 then yield n]
let sum = List.sum multiples
printfn "The sum of all multiples are: %d" sum


Some changes:

You don't need #light any more. I would also use |> more (this is more useful in more complex examples where it helps type inference.

open System
[for n in 1..1000 do if n % 3 = 0 || n % 5 = 0 then yield n]
|> List.sum
|> printfn "The sum of all multiples are: %d"

• What exactly does |> do? :) Oct 30, 2014 at 11:58
• @OverlyExcessive = a |> b is exactly the same as b(a) Oct 30, 2014 at 12:03
• well no, given b(a) you can do b(a).foo but not a |> b.foo Oct 30, 2014 at 15:42
• @Maslow I don't know F#, but wouldn't it be possibly to do (a |> b).foo? Oct 30, 2014 at 19:21
• yes, but he said exactly the same which is mostly accurate, but not entirely Oct 30, 2014 at 23:35

I think you should use sequence expressions only when you need them. In simple cases like yours, use existing functions, like List.filter. When combined with the pipe operation, your code could look like this:

let sum =
[1..1000]
|> List.filter (fun n -> n % 3 = 0 || n % 5 = 0)
|> List.sum

• What is the advantage of this over sequence expression? Is this more idiomatic :) ? Oct 30, 2014 at 17:06
• I think that it's simpler and that the structure of the computation (some initial collection, then filter, then sum) is immediately visible. Oct 30, 2014 at 17:10