Welcome to MSDN Blogs Sign in | Join | Help

Ben Vincent has blogged an excellent write up of his experiences with the IIS Search Engine Optimization (SEO) toolkit.

It’s well worth a read and illustrates how the tool can increase your sites relevance on search engines (and drive traffic to your site). After running the tool Ben’s site ends up getting as much referral traffic to Tassography.com in one month as he was getting for an entire year before running the tool!

Read his article and while you’re there, check out some of his photos, including the 5th most popular photo in the Facebook Bing competition.

It's been pointed out that I didn't need to write the extension method RaiseToThePowerOf. BigInt has it's own static Pow method. I blame code blindness.

So, with this in place Problem 25 is solved like this...

 

   1:  var ten = BigInt.FromInt32(10);
   2:  var nineninenine = BigInt.FromInt32(999);
   3:  var first1000DigitNumber = BigInt.Pow(ten, nineninenine);
   4:   
   5:  var answer = new Fibonacci().BigSequence
   6:                      .TakeWhile(x => x < first1000DigitNumber)
   7:                      .Count() + 1;
   8:   
   9:  return answer;  

 

... which is a bit better. Thanks bistok.

UPDATE: It's been pointed out that the extension method RaiseToThePowerOf isn't required. I've updated the C# solution to Problem 25 here, but left this post "as is".

This is part one of a three part post where myself and Giles Knap play around with C# and F# to see if we find anything useful or interesting in the comparison. You can find Part 1 of this series here.

Thanks for Playing Along

First up, I'd like to say thanks to those of you who commented on Part 1. KFarmer gave some great comments, and in particular I liked his suggestion for implementing GetPrimes in my C# SieveOfEratosthenes type. I've implemented GetPrimes2 and you can find this in the code on Codeplex.

I'd also like to thank Rick for his write-up. I'd like to respond to a few of Rick's comments. First up, I admit to being a "die hard C# fan" but I want to believe in F# too. Don't hold back - tell us where we can make the F# code look better or run faster - we want your input! Rick also mentions that we've written single core code. That's "by design". We wanted to concentrate on the latest RTM version of C# and the "out of the box" install of F#. Parallel coding practices are definitely important, but we're going to leave C# and F# parallel extensions to one side for the moment. We may well revisit this area in the future.

Limitations with Enumerable.Range

While implementing KFarmer's suggestions I reminded myself of something that annoys me with Enumerable.Range. I've had several situations where I'd like to create a range of something other than int. Also, I'd sometimes like to express my ranges in some other way than by saying "start at i and generate n integers". Sometimes I want to say "start at i and keep going until you get to j". Yes, I know that a for loop would do this for me, but I long for something like range expressions in F#.

OK, Back to the Problems

So, as you know from the previous post we're working our way though 6 problems from ProjectEuler.net. In this post we're going to look at the just one problem:

Problem 25 - What is the first term in the Fibonacci sequence to contain 1000 digits?

The source code for our solutions can be found at http://FSharpCSharp.codeplex.com. If you want to play along then you'll need to get the September 2008 CTP for F#.

Problem 25: What is the first term in the Fibonacci sequence to contain 1000 digits?

Full details of problem 25 can be found here.

To solve this problem you first need something that generates numbers from the Fibonacci sequence. Then, you want to find the first value in this sequence that's bigger than or equal to 10999 (the first number with 1000 digits). At least, that's what I set out to try and do, but with C# this proved to be a bit tricky. Let's start with Giles' F# solution.

F# Solution

Giles writes a function that generates numbers from the Fibonacci sequence, and keeps pulling on this until he finds a number that's bigger than 10999 and then returns the length of that sequence (plus one) to determine the answer to the problem:

   1:  #light
   2:   
   3:  open Microsoft.FSharp.Math
   4:   
   5:  let fibonacci =
   6:      let rec fibonacciInner (a:BigInt) (b:BigInt) =
   7:          seq { let c = a + b   
   8:                yield c  
   9:                yield! fibonacciInner b c }
  10:      seq { yield 1I  
  11:            yield 1I  
  12:            yield! fibonacciInner 1I 1I }  
  13:            
  14:  let answerProblem (upto:BigInt) =
  15:          ( fibonacci  
  16:          |> Seq.take_while (fun (elem) -> elem < upto)   // The take_while stops one short
  17:          |> Seq.length ) + 1                             // so increment result
  18:          
  19:  let solve() = answerProblem (10I ** 999I)

A simple solution to this problem. Giles had to explicitly type some variables as BigInt to ensure that they weren't inferred to be regular integer values, but other than that it pretty much reads as you'd expect it to.

C# Solution

I stumble at the first hurdle in C#. I want to define a variable that holds a value of 10999 but which type can I use? Int32 and Int64 (unsigned or otherwise) are inadequate. System.UInt64 will cater for a maximum value of 18,446,744,073,709,551,615. System.Decimal also falls short of the mark. Decimal can cope with a maximum value of 296-1.

So, what can you do if you need to deal with really large numbers in C#? Well, it turns out you can't. You're left with working around the problem, writing a type that can deal with large numbers, or borrowing a type from some language that does support really big numbers. J# is one such language, but we know F# is another. So, I ended up referencing FSharp.Core.dll and using the Microsoft.FSharp.Math.BigInt type in my solution!

   1:  public IEnumerable<BigInt> BigSequence
   2:  {
   3:      get
   4:      {
   5:          var a = BigInt.Zero;
   6:          var b = BigInt.One;
   7:          var c = a + b;
   8:   
   9:          while (true)
  10:          {
  11:              yield return c;
  12:   
  13:              c = a + b;
  14:              a = b;
  15:              b = c;
  16:          }                
  17:      }
  18:  }

So, I've managed to write a Fibonacci generator. But this isn't the end of my problems. I want to keep pulling values from this generator until I get a value that's bigger than 10999, but it's not clear how I go about defining a value of 10999. Math.Pow is the normal way of doing this, but Math.Pow deals with doubles and doesn't know how to deal with BigInt. So, I had to write my own function to raise a BigInt by a given power. I used a fast exponentiation method (rather than just performing n-1 multiplications) and defined it as an extension method on BigInt...

 

   1:  public static BigInt RaisedToThePowerOf(this BigInt value, int power)
   2:  {
   3:      if (power == 0)
   4:      {
   5:          return BigInt.One;
   6:      }
   7:   
   8:      // recursive call
   9:      var x = RaisedToThePowerOf(value, power / 2);
  10:   
  11:      if (power % 2 == 0)
  12:      {
  13:          return x * x;
  14:      }
  15:      
  16:      return value * (x * x);
  17:  }

 

... and from this was able to write my solution to the problem...

 

   1:  var ten = BigInt.FromInt32(10);
   2:  var first1000DigitNumber = ten.RaisedToThePowerOf(999);
   3:   
   4:  var answer = new Fibonacci().BigSequence
   5:                      .TakeWhile(x => x < first1000DigitNumber)
   6:                      .Count() + 1;

Phew! I got there in the end!

Conclusion

This isn't a competition - we're not looking for winners and losers. However, if this was a competition then it has to be a win for F#. The C# solution works, and I made it as pretty as possible, but it's a butt-ugly hack compared to F# where dealing with large numbers works out of the box. C# just wasn't up to the job when it came to dealing with big integers.

The one cool thing here is that I was able to borrow a type from F# very easily and with it get myself out of a sticky situation.

Feel free to play with our code, make it better, show us the error of our ways, or come up with even more elegant solutions that show one or other of the languages in their best light.

On a recently snowboarding holiday I found myself having some decidedly geeky conversations with a good friend of mine, Giles Knap. Giles is currently learning F# and was extolling its virtues while I did my best to defend C#, explaining to him that C# 3.0 included most of the functional language constructs that I was interested in.

At the end of the holiday the two of us came up with an idea. We both wanted to better understand each other's chosen programming language, and Giles wanted something that would help him learn F#. We were both familiar with a website called ProjectEuler.net which presents a number of mathematical questions and challenges people to solve them – generally be writing a small amount of code. So, we picked 6 questions from the ProjectEuler.net site and each coded up a solution; mine in C# and Giles' in F#.

What we were looking for was the descriptive power of the languages. The questions on ProjectEuler.net quite often have several solutions, where the most efficient solution is based upon knowing some mathematical shortcut (e.g. calculation of Fibonacci sequence is very much quicker if you happen to know about the golden ratio) but we weren't particularly interested in writing the very best performing code – rather, we wanted to write code that showed the language off in its best light and described to a reader exactly what was going on. That said, if the code wasn't able to generate an answer in a few seconds then it wasn't considered.

So, here are the 6 problems, from ProjectEuler.net, that Giles chose for us:

Problem 10 – Calculate the sum of all the primes below 2 million.

Problem 12 – What is the value of the first triangle number to have over five hundred divisors?

Problem 25 - What is the first term in the Fibonacci sequence to contain 1000 digits?

Problem 27 - Find a quadratic formula that produces the maximum number of primes for consecutive values of n.

Problem 36 - Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2

Problem 40 – Finding the nth digit of the fractional part of the irrational number.

I'll cover problems 10 and 12 here, and the others in subsequent posts. The source code for our solutions can be found at http://FSharpCSharp.codeplex.com. If you want to play along then you'll need to get the September 2008 CTP for F#.

Problem 10: Calculate the sum of all the primes below 2 million.

Full details of problem 10 can be found here.

To solve this problem you first need something that can generate primes. Then, with this, you can start summing them. The Sieve of Eratosthenes is a simple technique for finding all the prime numbers up to a specified integer, so in both the C# and F# solution we use this technique.

C# Solution

To generate primes I created a type that returned an IEnumerable<long>. This method returns an enumerator that will supply prime numbers up to some given maximum value:

 

The sieve works by creating nonprimes, an array of bool, and marking each position in the array as being a prime or not. All values are assumed to be prime (i.e. nonprime[n] = false) until marked as nonprimes (nonprime[n] = true). Whenever we find a prime then we mark all the multiples of that prime as being non-prime. We start marking multiples of the prime from the square of the prime because we can assume all other numbers below that value will have already been marked.

With this Prime number generator in place, the C# solution to problem 10 looks pretty simple:

By making use of the extension method Sum on IEnumerable<T> I can write a single line of code that creates my sieve, gets an enumeration of primes up to 2 million, and then sums them. This approach of writing sequence generators (methods that return IEnumerable<T>) is something that I've used throughout the C# solutions because it results in very simple and easy to understand solutions.

F# Solution

The F# solution to this problem takes the exact same approach:

In my opinion this solution benefits from a number of F# features. Firstly, it's way more compact. All those opening and closing brackets in the C# solution are missing. Yes, I could have dropped a number of them, but I tend to stick to StyleCop's rules for C# formatting. Secondly, the way that sequence expressions and sequences in general work in F# make it a very powerful language for solving any problem that deals with a sequence of numbers, such as those generated by the Sieve of Eratosthenes.

So, apart from some stylistic differences there isn't much difference between the C# and F# solutions to this problem.

Problem 12: What is the value of the first triangle number to have over five hundred divisors?

Full details of problem 12 can be found here.

Problem 12 deal with triangle numbers, and then working out how many divisors each of these numbers has.

C# Solution

The first thing I did was write code that generated triangle numbers. As with Problem 10, I did this by writing code that returned an IEnumerable<T>. Here is my Triangle Number generator:

  

This simple code will yield each a triangle number when the called "pulls" in the enumerator. Next, I needed something that would tell me each of the divisors that a given integer had. I did this by writing an extension method on the Int32 type that returned an IEnumerable<int> containing each divisor:

With my Triangle Number generator, and my GetDivisors extension method, I was finally able to solve this problem with the following code:

This comprehension syntax query is, in my opinion, pretty simple to read and does a good job of describing the solution to the problem.

F# Solution

Giles' F# solution also follows the same strategy as mine. Here is the code:

Conclusions (so far)

Maybe it's because we both chose similar ways of solving these problems, or maybe it's because these questions don't lend themselves to showing the language differences, but to me Problems 10 and 12 have equally readable and understandable representations in both C# and F#.

One big difference that we found between the C# and F# solutions was the performance. Using some simple timing code we observed that for Problem 10 the F# solution was approx 5 time slower than the C# solution. In addition the F# solution used a pretty large amount of memory. Why is this?

The F# collection types all support IEnumerable and you can build them with a sequence generating pattern which uses yield, just like C#. However, it turns out that there are some performance overheads with this approach. These sequence iterator blocks are compiled into state machines in C# (Jon Skeet has an article that explains how this happens) but in F# they use lambda functions. Using this approach in a nested loop is therefore very expensive!

The HubFS site proved invaluable in resolving this issue, for the full thread see http://cs.hubfs.net/forums/thread/9149.aspx. A total of 4 different solutions were suggested and tested. We selected the solution that had reasonable performance but still demonstrated an elegant way of using the F# sequence type (thanks to 'kvb' for the suggested code). We did find a solution that gives the same performance as C# but it was rejected as being too ugly! It's understood that this perf issue with sequence blocks will be addressed in a future release of F#.

Next Time

In the next post I'll be looking at 25 and 27. 25 is a particularly interesting Problem in that solving it in C# was tricky and, in fact, required a little help from F#. In the mean time, feel free to play with our code, make it better, show us the error of our ways, or come up with even more elegant solutions that show one or other of the languages in their best light.

Howard van Rooijen has just announced that the Release Candidate for StyleCop for ReSharper is now available.

If you like StyleCop (you do, don't you?) then you should definately take a look at this. Howard wants all your feedback, so go download StyleCop for ReSharper now.

I love writing code. Sometimes, though, not writing code is even better. Less code written means less to test, less to maintain, less to document etc etc.

Ben Vincent, Lead Program Manager for Windows Live Calendar, likes to spend his free time pretending to be a developer ;-) Actually, he's going a pretty good job because his pet project, Tassography.com, is looking pretty good in it's latest release.

I've been giving Ben some feedback on his code and what's particularly nice is when you can review someone's code and say "hey - that's good code, but why not throw it all away?"

In Ben's own words...

Thanks to Martin Peck a friend in the UK, I have rewritten the XML layer to speed up the loading of data, reduce the amount of memory it takes up and speed up the queries. Almost all the data queries use LINQ which has allowed me to delete 8 or so classes and an about a hundred lines of code. I'm getting another code review soon, so I'm sure I'll have some additional changes to work on!

You're welcome Ben. I'm looking forward to reviewing the code fully and showing you how little of it you needed to write :-)

Isn't it great when you spend all day sat in front of a PC and then get home and get asked to do the same thing? Over the last week I've been helping my wife set up a website designed to keep the residents of our village informed about the Community Plan that is being developed.

When she first asked me to help out, the developer in me wanted to open Visual Studio and hit File | New | Website... but I needed to produce something that a non-developer could update and maintain. In other words, I'd quite like my evenings back!

So, I thought I'd play with the Microsoft Office Live Small Business service. I have to say, I'm very impressed! The website designer gives you quite a lot of freedom, and allows you to fall back into writing HTML if you find it can't quite do what you want. However, I've not found many places where that was necessary.

Microsoft Office Live Small Business also provides a whole host of services around e-mail campaigns, site reports, contact management etc. On top of that, when you sign up you get free domain name registration.

I found Chris's unofficial Office Live developer blog useful in highlighting some of the more advanced features that the service supports, in particular his post about the new advanced design features. And, if you really need to, it's possible to take full control of the site design by using tools like Expressions.

Anyway, the finished site for Twyford Village Partnership can be found at http://www.twyfordvillage.com.

I've been using StyleCop within Microsoft for some time now. I'm a big fan, and really excited to see that it's just been released to the rest of the world.

Go find out about the release here.

There's a brand new release of the SDC Tasks library available.

A lot of the feedback (suggestions, patches etc) from CodePlex users has now been merged into the latest release, as well as a number of changes that have come from internal use of this library.

We were discussing the use of the new modifier in C# in my team today, and I was expressing my opinion that I don't like it. The new modifier is used to explicitly hide a method in a base class. I'm sure there are good reasons to do this, but generally the use of the new modifier rings alarm bells for me that the API design is wrong. If you're hiding and re-defining properties and methods using new in a public API then you could trip up consumers of your API and introduce subtle bugs into their code.

It all has to do with predictability and consistency in API usage. Let me explain.

Take the following type...

/// <summary>

/// Base type from which others will derive

/// </summary>

public class Base

{

/// <summary>

/// A non-virtual method that we weren't expecting anyone to override

/// </summary>

public void NonVirtualMethod()

{

Console.WriteLine("Hello from Base");

}

/// <summary>

/// A virtual method that we expect derived types to override

/// </summary>

public virtual void VirtualMethod()

{

Console.WriteLine("Hello from Base");

}    

}

This type declares two methods – NonVirtualMethod and VirtualMethod. The developer who designed this type wasn't expecting that anyone would need to override NonVirtualMethod. They did, however, expect that VirtualMethod might be overridden in a derived type.

Now we define a type that derived from Base...

/// <summary>

/// Type that derives from Base

/// </summary>

public class Derived : Base

{

/// <summary>

/// We're hiding NonVirtualMethod in Base and providing our own

/// </summary>

public new void NonVirtualMethod()

{

Console.WriteLine("Hello from Derived");

}

/// <summary>

/// We're overriding VirtualMethod to provide our own implementation

/// </summary>

public override void VirtualMethod()

{

Console.WriteLine("Hello from Derived");

}

}

I've highlighted the new modifier. The type Derived explicitly hides the base classes NonVirtualMethod and supplies its own. C# forces you to be explicit about this (which is good). This type also overrides the VirtualMethod using the override modifier.

Now, along comes a consumer of this API and writes the following code:

public static void Main(string[] args)

{

// create instance of Derived type

Derived foo = new Derived();

// reference the same instance of Derived using its base type Base

Base bar = foo;

// call the virtual method                                         

foo.VirtualMethod();

bar.VirtualMethod();

// call non-virtual method

foo.NonVirtualMethod();

bar.NonVirtualMethod();

// wait for use to hit a key

Console.WriteLine("Press any key to continue...");

Console.Read();

}

Now, can you tell me what you'd expect to see written to the Console here? I've created a single instance of the type Derived and called VirtualMethod and NonVirtualMethod in 2 ways; first using a reference declared as Derived and second through a reference declared as Base.

When you see on the console is this...

Hello from Derived

Hello from Derived

Hello from Derived

Hello from Base

Press any key to continue...

When we call VirtualMethod it doesn't matter how we call it – I get the implementation in the type Derived. This, I would suggest, is pretty obvious and predictable to most developers.

However, when NonVirtualMethod is called we get two different responses. It turns out that it makes a difference how we call NonVirtualMethod. When we call it via a reference of type Derived we get the implementation in the type Derived. When we call it using a Base type reference we get the implementation in Base.

This behaviour is totally understandable and there's nothing wrong with it, but for the consumer of this API was it expected? Are they expecting that an instance of Derived will behave differently when cast to its base type? When dealing with virtual methods you get consistent behaviour regardless of whether you're casting to the base type or not.

So, I would argue that using new in a public API is a bad thing. It could lead to subtle bugs within code that consumes your API and, as such, should be avoided.

Last week I attended and presented at the Microsoft Mediaroom Developers Conference in Boston.

The conference went really well, and I was surprised at how much I enjoyed presenting. I generally don't do much presenting, and I was standing up in front of a reasonably large audience, but it went well.

In fact, by pulling together my presentation and demos (ASP.NET "deep dive") I ended up solidifying my understanding about many of the things I was presenting. When you're about to stand up and talk in front of a group of developers you really need to fully understand what you're presenting. If you don't then you can get into all sorts of trouble when the Q&A starts.

SDC Tasks, a library of over 300 MSBuild tasks, has moved from its old (and soon to be demolished) home of GotDotNet to a new and shiny location on CodePlex. The new location is http://www.codeplex.com/sdctasks.

The CodePlex project hosts the latest release of SDC Tasks plus you can get access to the source code. Feel free to log bug and feature requests there too.

I'm currently in the process of setting up a new home for the SDC Tasks on CodePlex. As soon as the project is properly set up I'll announce the URL, but for the moment you can still access the code on GotDotNet. Alternatively, use the url www.BuildFramework.com which is an alias for the GDN project site at the moment and will become an alias for the CodePlex project site soon!

Andy Reeves has announced that he's leaving Microsoft. Andy was the creator and driving force behind the SDC Tasks library and SBF. It's a shame to see a great engineer like Andy leaving the Microsoft.

At the end of this week Microsoft UK will have one less brilliant developer. Giles Knap and his wife Natasha will be leaving Microsoft to travel around the world.

Giles has been instrumental in developing the Solution Build Framework, and much of the SDC Tasks library. He has been dedicated to the ideal that a developer should be able to walk up to some source code they've never seen and be able to build it and start debugging it after running a simple MSBuild script. SBF does that, and more, and Giles can take credit for that.

Giles and Nats will be missed here, but I'm going to keep an eye on the Giles 'n' Tish Travelog. You can read their first post here.

Have a great trip guys!

More Posts Next page »
 
Page view tracker