Re: Convert a small piece of code
- From: "Kaz Kylheku" <kkylheku@xxxxxxxxx>
- Date: 26 Nov 2005 10:54:04 -0800
Marco Minerva wrote:
> Hi all!
>
> Could anyone convert for me this piece of code, in C or Java?
Some Lisp code can be transliterated into /pseudo-code/ that resembles
these languages. Is that what you are looking for?
> (let ((g (* 2 (or (gethash word good) 0)))
> (b (or (gethash word bad) 0)))
C has no hash tables, so you have to invent that first. Show me the
declarations for your hash table library, and I can show you the code
that does these lookups.
> (unless (< (+ g b) 5)
The pseudo-code for this part would be:
if (g + b < 5) {
/* return failure indication: frequencies are too small */
} else {
/* see below */
}
But in the C language and java, adding two integers can overflow. So
that is why this is pseudo-code only: the syntax is C, but the
semantics are still Lisp. If we allow C semantics, then this program
suffers from strange bugs and limitations.
A more faithful translation into C or Java would require a bignum
library. Show me the bignum library interface, and I can write the code
....
> (max .01
> (min .99 (float (/ (min 1 (/ b nbad))
> (+ (min 1 (/ g ngood))
> (min 1 (/ b nbad)))))))))
C has no min or max functions. The usual trick is to write macros that
do this. These macros evaluate one of their arguments more than once,
which doesn't matter here. In C++, we can write a template function.
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
After this, we hit a wall. I have guessed that the B and G variables
are integers, but what are NBAD and NGOOD? I'm guessing they are
integers also, which just count totals.
What (/ B NBAD) does is compute B / NBAD. But this requires
fractional division. The Java and C languages have a dumb / operator
that only performs truncating integer division. In Lisp, integers are
really rational numbers, and when you divide them, you get an exact,
rational result.
The code here depends on that, because the quantity B (the frequency of
a particular word according to the bad table) is in all likelihood
smaller than NBAD (which I'm guessing is the total of all frequencies
of words in the bad table). Under the C division operator, the result
of this division will always be zero! We could cast the operands to
double and do floating-point math. But that is semantically different
from what is going on here and could yield different results. You can't
take liberties like that when translating numerical code, without doing
the appropriate analysis which justifies what you are doing.
Under Lisp, the result of the (/ B NBAD) division will be an an exact
rational number between zero and one. All of the arithmetic is exact up
until the point where the rational result is passed through the FLOAT
function which converts it into floating-point quantity, which is then
clamped into the range 0.01 and 0.99 using MIN and MAX.
Once again, show me your Java or C library for handling rational
numbers and I can write the code ...
And of course, the code returns either NIL or a floating point number.
There is no direct way to express that in C. You can return the result
by writing through a double * pointer that comes in as a parameter,
and also return an int flag. In Java you could probably return a null
reference to some numeric class instead of a basic type.
Also, we are assuming that the code is the body of a function, too. It
could actually just be an expression embedded in a larger expression.
In Lisp, expressions like the above can return value to the surrounding
expression. There isn't anything like that in C. The return statement
bails the entire function.
So you see, these languages are way too bogged down with limitations,
and a general lack of power, to translate the above code snippet in a
direct way.
.
- References:
- Convert a small piece of code
- From: Marco Minerva
- Convert a small piece of code
- Prev by Date: Re: CL idiom for Scheme's named let
- Next by Date: Re: Problem with ltk
- Previous by thread: Re: Convert a small piece of code
- Next by thread: Re: I'm working on yet another license
- Index(es):
Relevant Pages
|
|