Fibs in C
1
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.
2
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];
}
3
Name:
Anonymous
2008-06-14 11:11
fibslist[fibslist_last-1]+fibslist[fibslist_last-2];
I lol'd.
4
Name:
Anonymous
2008-06-14 20:05
>>1,2
Same person that ruined his own thread by
/threading itself
5
Name:
Anonymous
2008-06-14 23:39
>>3
that's actually how haskell does it.
6
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.
7
Name:
Anonymous
2008-06-15 4:16
8
Name:
Anonymous
2008-06-15 7:47
9
Name:
Anonymous
2008-06-15 9:29
>>8
No, that's how SCHEME does it.
10
Name:
Anonymous
2008-06-15 10:27
>>2
C for Haskell programmers?
11
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);
}
12
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);
}
13
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
14
Name:
Anonymous
2008-06-15 10:45
>>12
Please use statics instead of globals
15
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.
16
Name:
Anonymous
2008-06-15 11:06
Will fibarray be filled with zeroes in
>>12 ?
Anyway your implementation destroys stack, good job.
17
Name:
Anonymous
2008-06-15 11:23
Anyway your implementation destroys stack, good job.
I lol'd
18
Name:
12
2008-06-15 11:56
>>16
I think static arrays are always filled with zerores.
>>17
So did I.
19
Name:
Anonymous
2008-06-15 11:56
>>17
Is that you again,
>>3 ?
20
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];
}
21
Name:
Anonymous
2008-06-15 12:08
22
Name:
Anonymous
2008-06-15 12:11
23
Name:
Anonymous
2008-06-15 12:13
24
Name:
Anonymous
2008-06-15 12:30
25
Name:
Anonymous
2008-06-15 12:38
malloc(sizeof(fibs));
26
Name:
Anonymous
2008-06-15 13:37
map f [NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL]
27
Name:
Anonymous
2008-06-15 13:47
>>20
int last = 1;
Pig disgusting
28
Name:
Anonymous
2008-06-15 14:07
29
Name:
Linus Tornballs
2008-06-15 14:50
>>28
I'm right because I'm right.
30
Name:
AST Stu Tanenbaum
2008-06-15 14:57
>>29
Ergo your wrong bitch.
31
Name:
Anonymous
2008-06-15 15:07
fibs fibs fibs ((((fibs)))) fact
32
Name:
Anonymous
2008-06-15 15:09
>>30
I will, along with my right bitch.
33
Name:
Anonymous
2008-06-15 15:11
>>31
You are not a programmer, what are you doing here?
34
Name:
Anonymous
2008-06-16 7:31
35
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.
36
Name:
Anonymous
2008-06-16 15:03
37
Name:
Anonymous
2008-06-16 15:08
>>35
That is not memoizing.
38
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);
}
39
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);
}
40
Name:
Anonymous
2008-06-16 15:36
int fib(int i) __attribute__((const)){
fixed.
41
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?
42
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.
43
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.
44
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)).
45
Name:
Anonymous
2008-06-16 18:58
>>43
i guess i'm not the only person on /prog/ who actually knows c.
lol
46
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.
47
Name:
Anonymous
2008-06-16 22:43
fib = [ x | x=a+b, (a,b) <- fibs]
48
Name:
Anonymous
2008-06-16 22:54
>>46
15? That's a rather
estimate.
49
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.
50
Name:
Anonymous
2008-06-16 23:21
>>49
Well, obviously. You, me and the Sussman.
51
Name:
Anonymous
2008-06-16 23:42
>>50
Wait, I'm neither you nor the other guy.
OH SHI-
I'M THE FUCKING SUSSMAN!
52
Name:
GJS
!!aWqau2KjG0OkKV7
2008-06-17 1:12
I am the real SUSSMAN
53
Name:
Anonymous
2008-06-17 8:53
Email this thread to GJS immediately.
54
Name:
Anonymous
2008-06-17 8:55
Emailing /prog/ threads to GJS is unscientific and ultimately destructive.
55
Name:
Anonymous
2009-08-16 23:17
Lain.
56
Name:
Anonymous
2009-08-16 23:27
Lain.
58
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!
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
60
Name:
bampu pantsu
2012-05-29 3:50
bampu pantsu