For those of you who don't know, "Project Euler is a series of challenging mathematical/computer programming problems..." (ProjectEuler.net). The mathematical nature of these problems make them a great fit for the functional paradigm.
Here's the first problem:
"Add all the natural numbers below one thousand that are multiples of 3 or 5." Additional details include the following simple example: "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."
With the help of the simple example, I created a test that looked like this:
module Problem1Tests open NUnit.Framework [<TestFixture>] type FindPrimes2__When_Getting_Sum_Of_Multiples_Of_3_And_5_to_a_max_number_of_10 () = [<Test>] member this.should_return_sum_of_23 () = let result = Problem1.GetSumOfMultiplesOf3And5(10) Assert.AreEqual(23, result)
After a few refactoring sessions, my final code looked like this:
module Problem1 let GetSumOfMultiplesOf3And5 max = seq{3..max-1} |> Seq.fold(fun acc number -> (if (number % 3 = 0 || number % 5 = 0) then acc + number else acc)) 0
Have you found good Code Kata exercises? If so, I'd love to see them. Also, let me know if you have a better solution for the problem above.