Managed meets functional

Blog about programming and having fun with .Net

About me

 Venice, 2009

profile for Alexander Galkin on Stack Exchange, a network of free, community-driven Q&A sites

Project Euler

Greetings here in my blog!
My name is Alexander Galkin. I was born 1979 in Kazan, Russia, where I graduated in child medicine.
Since 2001 I live in Hamburg, Germany and work as a freelancer software and database architect and trainer for Microsoft technologies.

 Microsoft Certified Trainer
Microsoft Certified Professional Developer
MCTS Logo
MCITP Logo

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

Why C++ is not good as the first programming language

This term I teach the course "Introduction into Programming with C++" for the first-year students of Engineering Sciences. 

The course was requested by the University and targets the students with no prior knowledge of programming. The choice of C++ as the language of the course was not mine, this is the default language to teach OOP at our university (Hamburg University of Technology), for the professor in charge is doing his research primarily using C++. So, the decision to use C++ as the first language was more or less imposed on me by the dean office.

During the very first session I provided students with several motivating examples. One of those was the classical task about the chessboard and the wheat beads, where  one Vizir truly enchanted by the wonderful game of chess asks the inventor for a decent reward and the latter asks him to put one wheat bead on the first field, two beads on the second fields etc. until every field is covered. This seemingly trivial task results in a very high number which is not so easy to compute directly using a brute force approach (without deriving the summation formula for this geometric row).

So, we implemented this task in several languages. Here is the one-line implementation in F# (MS version of oCaml for .Net):

let rice = 
     [0 .. 63] |> List.map (fun x -> 2I ** x)  |> List.sum |> Dump

The equivalent C# (and with small changes C++) code is much longer and requires much more things to keep an eye on:

void Main()
{
	ulong sum = 0;
	for(int i = 0;i < 64; i++) 
	{
	  sum+= power2(i);
	  sum.Dump();
	}
	sum.Dump();
}

// Define other methods and classes here
ulong power2(int power)
{
   ulong answer = 1;
   for(int i=0; i < power; i++) answer=answer * 2L;
   return answer;
}

This is why I do agree with the widely spread opinion that Computer Science and especially the algorithms should be first taught using a non-imperative language, like Haskell, oCaml, Erlang, Scala or F#.


Permalink | Comments (0) | Post RSSRSS comment feed
Comments are closed