El 2 de abril de 2010 17:11, Bradley Lucier <lucier@math.purdue.edu> escribió:
This function in C
int distance_point_point(struct point *p1, struct point *p2) {
return sqrt( pow( p1->x - p2->x, 2) + pow( p1->y - p2->y, 2) );
}
is translated quite literally into (Gambit) scheme as
(define (distance-point-point a b)
(##flonum.->fixnum (flsqrt (fl+ (flexpt (fixnum->flonum (point-x a)) 2.)
(flexpt (fixnum->flonum (point-y a)) 2.)))))
The C type inference rules, the automatic conversion of int to double and back, and the header <math.h> takes care of all of that.
And you're allocating manipulating lists in the Scheme version, but using an array in the C version, which you could also do in the scheme version.
So these codes are in only a rough sense "equivalent".
If you add the declarations
(declare (standard-bindings)(extended-bindings)(block)(not safe))
then things should go a bit faster.
Brad