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

Pages: 1-4041-

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.

Name: Anonymous 2008-06-16 15:40

>>38
This is why I hate programming -- you do something small and get big effects. Where's the logic in this?

Name: Anonymous 2008-06-16 17:40

>>39
static fibs[2] = {0, 1};
fibs = realloc(fibs, i)
What compiler supports this kind of absurdity?

>>41
It's pretty obvious if you understand what the code is doing, which isn't hard at all in >>12,38's case.

Name: Anonymous 2008-06-16 18:24

>>42
wow, i didn't think anyone here would notice that. i guess i'm not the only person on /prog/ who actually knows c.
it's trivial to fix if you know why it doesn't work.

Name: Anonymous 2008-06-16 18:37


?- dynamic fib/2
fib(0,0).
fib(1,1).
fib(N,A):- N > 0, N1 is N-1, N2 is N-2, fib(N1,A1),
           fib(N2,A2), A is A1 + A2, asserta(fib(N,A)).

Name: Anonymous 2008-06-16 18:58

>>43
i guess i'm not the only person on /prog/ who actually knows c.
lol

Name: Anonymous 2008-06-16 21:55

>>43
There's 15 people on /prog/ and, from what I've seen, at least 6 of us know C.

Name: Anonymous 2008-06-16 22:43

fib = [ x | x=a+b, (a,b) <- fibs]

Name: Anonymous 2008-06-16 22:54

>>46
15? That's a rather estimate.

Name: Anonymous 2008-06-16 23:01

>>48
A rather estimate indeed. I'm convinced that no more than 3 different posters actually visit this board.

Name: Anonymous 2008-06-16 23:21

>>49
Well, obviously. You, me and the Sussman.

Name: Anonymous 2008-06-16 23:42

>>50
Wait, I'm neither you nor the other guy.

OH SHI-

I'M THE FUCKING SUSSMAN!

Name: GJS !!aWqau2KjG0OkKV7 2008-06-17 1:12

I am the real SUSSMAN

Name: Anonymous 2008-06-17 8:53

Email this thread to GJS immediately.

Name: Anonymous 2008-06-17 8:55

Emailing /prog/ threads to GJS is unscientific and ultimately destructive.

Name: Anonymous 2009-08-16 23:17

Lain.

Name: Anonymous 2009-08-16 23:27

Lain.

Name: Anonymous 2010-12-17 1:37

Are you GAY?
Are you a NIGGER?
Are you a GAY NIGGER?

If you answered "Yes" to all of the above questions, then GNAA (GAY NIGGER ASSOCIATION OF AMERICA) might be exactly what you've been looking for!

Name: Sgt.Kabukiman 2012-05-24 6:06

All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy

Name: bampu pantsu 2012-05-29 3:50

bampu pantsu

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