The more things change the more they stay the same...

Submitted by Greg Buchholz
on December 14, 2006 - 1:25am

After we correct for the algorithmic differences, is there really that much difference between...

fibo 0 = 1 
fibo 1 = 1 
fibo x = fibo (x-1) + fibo (x-2)

...and...

function fibo (x)
{
    switch (x) {
        case 0:  return 1;
        case 1:  return 1;
        default: return fibo(x-1) + fibo(x-2);
    }
}

...and...

sub fibo
{   my $x = shift;
    $x == 0 ? 1 :
    $x == 1 ? 1 :
    fibo($x-1)+fibo($x-2)
}

...and...

template<class T> T fibo(T x)
{
    switch (x) {
        case 0:  return 1; 
        case 1:  return 1; 
        default: return fibo(x-1) + fibo(x-2);
    }
}

...and...


(defun fibo (x)
  (case x (0 1)
          (1 1)
          (t (+ (fibo (- x 1)) (fibo (- x 2))))))

...and...

def fibo(x)
  case x
    when 0: 1
    when 1: 1
    else fibo(x-1) + fibo(x-2)
  end
end


...and...

fun fibo x = case x of
                0 => 1
              | 1 => 1
              | n => fibo(n-1) + fibo(n-2)


...and...

(define (fibo x)
  (case x ((0 1) 1)
          (else (+ (fibo (- x 1)) (fibo (- x 2))))))

update: And the languages are: Haskell, Javascript, Perl, C++, Common Lisp, Ruby, SML, and Scheme.

Not really--they're all not P

Anonymous (not verified)
on
December 14, 2006 - 4:12pm

Not really--they're all not Python. :-)

They actually look better now

Doug Tolton (not verified)
on
December 14, 2006 - 5:41pm

After you applied the formatting most of those examples look better to me than the Haskell syntax. I like the use of indenting to indicate relationships, particularly that a block of code all belongs to a function. That seems to be a well established programming idiom in almost every language. Apparently most other people like it too.

The funny thing about the original example is that he uses long function names and long variable names to make the target languages look worse than they actually are.

Thanks for re-writing them. :)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.