logo
Published on KernelTrap (http://kerneltrap.org)

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

By Greg Buchholz
Created Dec 14 2006 - 01:25

After we correct for the algorithmic differences [1], 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.


Source URL:
http://kerneltrap.org/node/7475