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-14 11:01

int fibs(int no){
    while(no>fibslist_last){
        fibslist_last++;
       
        fibslist[fibslist_last]=
            fibslist[fibslist_last-1]+fibslist[fibslist_last-2];
    }
    return fibslist[no];
}

Name: Anonymous 2008-06-14 11:11

            fibslist[fibslist_last-1]+fibslist[fibslist_last-2];
I lol'd.

Name: Anonymous 2008-06-14 20:05

>>1,2
Same person that ruined his own thread by /threading itself

Name: Anonymous 2008-06-14 23:39

>>3
that's actually how haskell does it.

Name: Anonymous 2008-06-15 2:57

>>4
I am >>2 and I can assure you that >>1 is some other person, definitely not me.

Also I forgot int fibslist[0x100]={0,1,},fibslist_last=1;, silly me.

Name: Anonymous 2008-06-15 4:16

>>5
Uhm, no?

Name: Anonymous 2008-06-15 7:47

>>7
yes.

Name: Anonymous 2008-06-15 9:29

>>8
No, that's how SCHEME does it.

Name: Anonymous 2008-06-15 10:27

>>2
C for Haskell programmers?

Name: Anonymous 2008-06-15 10:32

int fibarray[MAX_FIBS] = {1,1};

int fib(int n)
{
   return fibarray[n] ? fibarray[n] : fib(n-1) + fib(n-2);
}

Name: Anonymous 2008-06-15 10:39

Fuck.

int fibarray[MAX_FIBS] = {1,1};

int fib(int n)
{
   return fibarray[n] ? fibarray[n]
                      : fibarray[n] = fib(n-1) + fib(n-2);
}

Name: Anonymous 2008-06-15 10:43


int fib(int n)
{
  switch(n){
    case 1: return 1;
    case 2: return 1;
    case 3: return 2;
...
  }
}

ENTERPRISE TURKEY ALGORITHM

Name: Anonymous 2008-06-15 10:45

>>12
Please use statics instead of globals

Name: Anonymous 2008-06-15 10:56

>>14
I's not a global, it's a module variable. At least it would be, if C had modules.

Name: Anonymous 2008-06-15 11:06

Will fibarray be filled with zeroes in >>12?

Anyway your implementation destroys stack, good job.

Name: Anonymous 2008-06-15 11:23

Anyway your implementation destroys stack, good job.
I lol'd

Name: 12 2008-06-15 11:56

>>16
I think static arrays are always filled with zerores.

>>17
So did I.

Name: Anonymous 2008-06-15 11:56

>>17
Is that you again, >>3?

Name: Anonymous 2008-06-15 12:05

OMG OPTIMIZED

int fibarray[MAX_FIBS] = {1,1};
int last = 1;

int fib(int n)
{
   while(last < n) fibarray[last+1] = fibarray[last++] + fibarray[last-1];
   return fibarray[n];
}

Name: Anonymous 2008-06-15 12:08

>>20
fib(-1);

Name: Anonymous 2008-06-15 12:11

>>20,12,2.
fib(-1);

Name: Anonymous 2008-06-15 12:13

>>22
fib(-1);

Name: Anonymous 2008-06-15 12:30

>>21-23
memset(0,0,1);

Name: Anonymous 2008-06-15 12:38

malloc(sizeof(fibs));

Name: Anonymous 2008-06-15 13:37

map f [NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL]

Name: Anonymous 2008-06-15 13:47

>>20
int last = 1;
Pig disgusting

Name: Anonymous 2008-06-15 14:07

>>27
Why?

Name: Linus Tornballs 2008-06-15 14:50

>>28
I'm right because I'm right.

Name: AST Stu Tanenbaum 2008-06-15 14:57

>>29
Ergo your wrong bitch.

Name: Anonymous 2008-06-15 15:07

fibs fibs fibs ((((fibs)))) fact

Name: Anonymous 2008-06-15 15:09

>>30
I will, along with my right bitch.

Name: Anonymous 2008-06-15 15:11

>>31
You are not a programmer, what are you doing here?

Name: Anonymous 2008-06-16 7:31

>>31
Invalid LISP code.

Name: Anonymous 2008-06-16 14:57

int fib(int i) {
  if(i<2)
    return i;
  else
    return fib(i-1) + fib(i-2);
}

Sorry to break up the trolling, but this is what I would do.

Name: Anonymous 2008-06-16 15:03

>>35
You are trolling.

Name: Anonymous 2008-06-16 15:08

>>35
That is not memoizing.

Name: Anonymous 2008-06-16 15:14

>>12,16
If you invert the call order, it only destroys half as much stack.

int fibarray[MAX_FIBS] = {1,1};

int fib(int n)
{
   return fibarray[n] ? fibarray[n]
                      : fibarray[n] = fib(n-2) + fib(n-1);
}

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);
}

Name: Anonymous 2008-06-16 15:36

int fib(int i) __attribute__((const)){
fixed.

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