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

scheme vs C

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

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

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

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

Name: Anonymous 2010-10-08 8:25

>>1
static int r = 1;
That's the best idea ever!

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

Name: Anonymous 2010-10-08 8:58

What's wrong with the static?

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.

Name: Anonymous 2010-10-08 9:30

>>5
That's the best idea ever!
Thank you!

Name: Anonymous 2010-10-08 11:47


//works always
int fac(int n) {
        if (n == 1)
            return r;
        return n*fac(n-1);
}

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.

Name: Anonymous 2010-10-08 12:19

>>10
enjoy your non-tail-recursion, ``faggot''.

Name: Anonymous 2010-10-08 12:39

>>10
return r;
This shit will never work.

Name: Anonymous 2010-10-08 12:59

>>13
#define r 1

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

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

Name: Anonymous 2010-10-08 16:17

>>16
Perl 6?

Name: Anonymous 2010-10-08 18:03

>>17
Yeah. I should have done this to make it callable:
{[*] 1..$_}

Name: Anonymous 2010-10-08 19:20

>>14
Now you look like a C developer trying to imitate a PHP developer.

Name: Anonymous 2010-10-08 21:03

What about

int fac(int n) { return n ? n * fac( n - 1 ) : 1; }

?

Name: Anonymous 2010-10-08 21:11

>>20
What about INT_MAX?

Name: Anonymous 2010-10-08 21:27

>>21

long fac(int n) { return n ? n * fac( n - 1 ) : 1; }

Name: Anonymous 2010-10-08 22:16

>>20
It's not tail recursive is what.

Name: Anonymous 2010-10-08 22:27

>>23
Normal compiler doesn't care and optimize it to loop anyway.

Name: sage 2010-10-08 23:03

sage for fibs

Name: Anonymous 2010-10-09 9:41

So why is the static a bad idea?

Name: Anonymous 2010-10-09 10:09

>>26
because you touch yourself at night

Name: Anonymous 2010-10-09 17:00

>>26
Because knowing C is why.

Name: Anonymous 2010-12-09 3:53

Name: Anonymous 2011-01-31 19:57

<-- check em dubz

Name: Anonymous 2011-02-03 8:11


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