Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Fibs in C

Name: Anonymous 2008-06-14 10:50

How can you write the Haskell fibs in C? I mean the

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

fibs.

Name: Anonymous 2008-06-16 15:35

>>35,37
fixed:
int fib(int i){
  static int fibmax = 1;
  static fibs[2] = {0, 1};
  if(i > fibmax)
   for((fibs = realloc(fibs, i)) || abort("realloc() failed."); fibmax <= i;
     ++fibmax) fibs[fibmax] = fibs[fibmax - 1] + fibs[fibmax - 2];
  return fibs[i];
}


or if the compiler is smart:
int fib(int i) __attribute__((pure)){
 return i < 1 ? 0 : i < 3 ? 1 : fib(i - 1) + fib(i - 2);
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List