Geeks With Blogs
dev.marzdog Mike Roberts Development (+) Blog

Project Euler is a awesome site.   When working with a new language it can be tricky to find problems that need solving, that are more complex than "Hello World" and simpler than a full blown application. Project Euler gives use just that, cool and thought provoking problems that usually don't take days to solve. 

I've solved a number of questions with C# and some with Java.  BTW, I used Java because it had BigInteger support before .Net.

A couple weeks ago, back when winter had a firm grip on Columbus, OH, I began playing (researching) with F#.  I began with Problem #1 from Project Euler.  I started by looking at my solution in C#.

Here is my solution in C#.

   1:  using System;
   2:  using System.Collections.Generic;
   3:   
   4:  namespace Problem001
   5:  {
   6:      class Program
   7:      {
   8:          static void Main(string[] args)
   9:          {
  10:              List<int> values = new List<int>();
  11:   
  12:              for (int i = 1; i < 1000; i++)
  13:              {
  14:                  if (i % 3 == 0 || i % 5 == 0)
  15:                      values.Add(i);
  16:              }
  17:              int total = 0;
  18:   
  19:              values.ForEach(v => total += v);
  20:   
  21:              Console.WriteLine(total);
  22:              Console.ReadKey();
  23:          }
  24:      }
  25:  }

 

Now, after much tweaking and learning, here is my solution in F#.

 

   1:  open System
   2:   
   3:  let calc n = 
   4:      [1..n]
   5:      |> List.map (fun x -> if (x % 3 = 0 || x % 5 = 0) then x else 0)
   6:      |> List.sum
   7:   
   8:  let main() = 
   9:      calc 999
  10:      |> printfn "result = %d"
  11:      Console.ReadKey(true) |> ignore 
  12:   
  13:  main()

Just this little example highlights some cool things about F#.

  • Type inference. F# infers the type of a value.  In the C# code above we declare a number of variables, the list, and a couple ints.  F# does not require this, it infers the calc (a function) accepts a int and returns a int.
  • Great built in functionality for Lists.  List.map for example.

BTW, I don’t think I’m spilling the beans by giving away the code for Problem 1.  It by far is the easiest question, IMHO, solved by 92,000+ people.

Next I’ll look into writing a class library with F#.

Posted on Thursday, March 25, 2010 4:36 PM | Back to top


Comments on this post: Playing with F#

# re: Playing with F#
Requesting Gravatar...
I'd use

List.filter (fun x -> x % 3 = 0 || x % 5 = 0)

instead of

List.map (fun x -> if (x % 3 = 0 || x % 5 = 0) then x else 0)
Left by Yassin on Mar 25, 2010 6:04 PM

# re: Playing with F#
Requesting Gravatar...
Makes sense, as soon as I saw it. I've not spent enough time with f# yet. Thanks.
Left by mroberts on Mar 25, 2010 7:53 PM

# re: Playing with F#
Requesting Gravatar...
For the C# couldn't you just do this.

Console.WriteLine(Enumerable.Range(1, 999).Where(i => i % 3 == 0 || i % 5 == 0).Sum());

or this if you want to pass in a param for the range.

Func<int, int> calc = (i) => Enumerable.Range(1, i).Where(x => x % 3 == 0 || x % 5 == 0).Sum();
Console.WriteLine(calc(999));
Left by BT on Mar 26, 2010 9:31 AM

# re: Playing with F#
Requesting Gravatar...
Or do it all in one go.

let calc n =
[1..n]
|> List.fold (fun x y -> let a = if (y % 3 = 0 || y % 5 = 0) then y else 0
x + a) 0

.Aggregrate for C# version by BT above.
Left by Deeny D on Mar 26, 2010 12:22 PM

# re: Playing with F#
Requesting Gravatar...
Thanks for the comments, all good suggestions. I need to look into List.Fold.
Left by mroberts on Mar 30, 2010 10:25 AM

Comments have been closed on this topic.
Copyright © mroberts | Powered by: GeeksWithBlogs.net