Re: Convert a small piece of code



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.

.



Relevant Pages

  • Re: Some kind of Lisp targeting J2ME?
    ... >> I'm not sure I understand you but if you are talking about translation ... >> between Lisp names and Java names then you don't need to worry. ... and bang-bang will be translated into bangBang. ...
    (comp.lang.lisp)
  • Re: Some kind of Lisp targeting J2ME?
    ... > I'm not sure I understand you but if you are talking about translation ... > between Lisp names and Java names then you don't need to worry. ... > translated into Java conventions and names that follow Java ...
    (comp.lang.lisp)
  • Re: Is anything easier to do in java than in lisp?
    ... And any experienced lisp ... in java generally you hardly ever use ... static String showInputDialog(Component parentComponent, ... which was about generic sequences (lists ...
    (comp.lang.java)
  • Re: Is anything easier to do in java than in lisp?
    ... And any experienced lisp ... in java generally you hardly ever use ... static String showInputDialog(Component parentComponent, ... which was about generic sequences (lists ...
    (comp.lang.lisp)
  • Re: Lisp article at IBM
    ... And one reason why Lisp isn't as popular as Java is because it is difficult ... with such ease and elegance. ... Likewise, while there are a few success stories based on lisp, these seem very ...
    (comp.lang.lisp)