scheme vs C
1
Name:
Anonymous
2010-10-08 7:21
THE FUNDAMENTAL DIFFERENCE BETWEEN SCHEME AND C
;; works always
;;
(define (fac n)
(define (iter n c)
(if (< c 1)
n
(iter (* n c) (- c 1))))
(iter 1 n))
/* works once
*/
int fac(int n) {
static int r = 1;
if (n < 1) {
return r;
} else {
r = r * n;
fac(n-1);
}
}
2
Name:
Anonymous
2010-10-08 7:28
Portable C89:
int fac(int n) {
int a[8] = {1, 1, 2, 6, 24, 120, 720, 5040};
assert(0 <= n && n <= 7);
return a[n];
}
3
Name:
Anonymous
2010-10-08 7:37
Expert C:
void _(void) {
for (fork(); fork(); fork()) fork;
}
int fac(int n) {
atexit(_);
int a[] = {1, 1, 2, 6, 24, 120, 720, 5040};
return (n < 0 || n > 7) ? exit(n) : n[a];
}
4
Name:
Anonymous
2010-10-08 7:38
; Works slow.
(define (printfacs n times)
(display (fac n))
(printfacs n (- times 1))
// Works fast.
void printfacs(int n, uint64_t times) {
while (times--) {
printf("%d\n", fac(n)); n = 0;
}
}
5
Name:
Anonymous
2010-10-08 8:25
>>1
static int r = 1;
That's the best idea ever!
6
Name:
Anonymous
2010-10-08 8:54
>>5
In my experience with hardcore Lispers, that's not too far off from their mentality.
*writes shit code that doesn't work* OH MY, C IS TERRIBLE! I WILL GO BACK TO LITHP
7
Name:
Anonymous
2010-10-08 8:58
What's wrong with the static?
8
Name:
Anonymous
2010-10-08 8:59
The main difference here is that in C, you have to use a bignum library to make it truly general, in Lisp, bignums come for free when you go over fixnum's max size. This is especially important for factorials since they grow big very fast. The fixnum->bignum upgrade does come at the cost of extra typechecks at runtime (unless you force it into one or the either). Which is faster depends on the size of the argument. Also, OP's C example is nothing like I would write it. C is mostly imperative, so there's no shame in doing it the imperative way. There's no guaranteed TCO in C.
9
Name:
Anonymous
2010-10-08 9:30
>>5
That's the best idea ever!
Thank you!
10
Name:
Anonymous
2010-10-08 11:47
//works always
int fac(int n) {
if (n == 1)
return r;
return n*fac(n-1);
}
11
Name:
Anonymous
2010-10-08 11:51
>>10
That's not even in the right language, and it returns the wrong answer for n > 12 on most common systems.
12
Name:
Anonymous
2010-10-08 12:19
>>10
enjoy your non-tail-recursion,
``faggot'' .
13
Name:
Anonymous
2010-10-08 12:39
>>10
return r;
This shit will never work.
14
Name:
Anonymous
2010-10-08 12:59
15
Name:
Anonymous
2010-10-08 13:04
>>10
revised for accuracy
int fac(int n){
int r=1;
s:
if(n==0)
return r;
r = r*n;
n--;
goto s;
}
16
Name:
Anonymous
2010-10-08 14:21
Tail-recursive:
int fact(int n) { fact_r(1, n); }
int fact_r(int t, int n) {
if(n==1)
return t;
return fact_r(t*n, n-1);
}
Reduction:
[*] 1..$n
17
Name:
Anonymous
2010-10-08 16:17
18
Name:
Anonymous
2010-10-08 18:03
>>17
Yeah. I should have done this to make it callable:
{[*] 1..$_}
19
Name:
Anonymous
2010-10-08 19:20
>>14
Now you look like a C developer trying to imitate a PHP developer.
20
Name:
Anonymous
2010-10-08 21:03
What about
int fac(int n) { return n ? n * fac( n - 1 ) : 1; }
?
21
Name:
Anonymous
2010-10-08 21:11
22
Name:
Anonymous
2010-10-08 21:27
>>21
long fac(int n) { return n ? n * fac( n - 1 ) : 1; }
23
Name:
Anonymous
2010-10-08 22:16
>>20
It's not tail recursive is what.
24
Name:
Anonymous
2010-10-08 22:27
>>23
Normal compiler doesn't care and optimize it to loop anyway.
25
Name:
sage
2010-10-08 23:03
sage for fibs
26
Name:
Anonymous
2010-10-09 9:41
So why is the static a bad idea?
27
Name:
Anonymous
2010-10-09 10:09
>>26
because you touch yourself at night
28
Name:
Anonymous
2010-10-09 17:00
>>26
Because knowing C is why.
30
Name:
Anonymous
2010-12-09 3:53
33
Name:
Anonymous
2011-01-31 19:57
<-- check em dubz
34
Name:
Anonymous
2011-02-03 8:11