Wednesday, August 6, 2014

Programming without the programming language

I agree with Jon Skeet's assertion that it's better to know the idiomatic way to program with the programming language. There are cases though that using something fancy language operation like compounding a value to another variable, makes the solution look non-intuitive




I forgot how my old Fibonacci solution was intuitive to me:

static IEnumerable<int> Fibonacci()
{
    int a = 0,  b = 1;
                         
    for(;;)
    {        
        yield return a;
        int n = a;
        a += b;
        b = n;          
    }
}


Perhaps making a language-driven solution e.g., increment, makes something non-intuitive in the long run and hard to reason out


When I tried to plot the values of that old fibonacci solution, this is how a bit jumbled-looking the values are, read this from up to down, then left to right

A 0 1 1 2 3 5
B 1 0 1 1 2 3
Y 0 1 1 2 3 5
N 0 1 1 2 3 5




Then when I wanted to recall that fibonacci code off the top of my head, sadly I can't, so I try solve it with a pen and a paper, following is what I came up with, and it's a little different from the old solution I've made before. This is one of few instances where not having eidetic or retentive memory is good, the brain is flushing out old memories and it is stimulated to be creative on coming up with new and elegant approaches. Read the following from up to down, then left to right


Pencil-and-paper solution:


A 0 1 1 2 3 5
B 1 1 2 3 5 8
Y 0 1 1 2 3 5
N 1 2 3 5 8 13


The values' progression look symmetrical and it maps nicely to code too:

static IEnumerable<int> Fibonacci()
{
    int a = 0, b = 1;
      
    for(;;) 
    {      
        yield return a;
        int n = a + b;
        a = b;
        b = n;   
    }       
}


The only drawback of not having eidetic memory is your stock knowledge tend to decrease or get rusty. But who needs eidetic memory when you can store your knowledge to blog? Another advantage of having a blog is you can compare if your new approach improved

No comments:

Post a Comment