From: Marco Minerva
Subject: Convert a small piece of code
Date: 
Message-ID: <43861862$0$16208$4fafbaef@reader3.news.tin.it>
Hi all!

Could anyone convert for me this piece of code, in C or Java?

(let ((g (* 2 (or (gethash word good) 0)))
      (b (or (gethash word bad) 0)))
   (unless (< (+ g b) 5)
     (max .01
          (min .99 (float (/ (min 1 (/ b nbad))
                             (+ (min 1 (/ g ngood))
                                (min 1 (/ b nbad)))))))))

where "word" is a key for the hash tables "good" and "bad", "ngood" and 
"nbad" are two numbers.

Thanks in advance!
-- 
Marco Minerva, ·············@tiscali.it 

From: matteo d'addio 81
Subject: Re: Convert a small piece of code
Date: 
Message-ID: <1132867216.242939.303320@g44g2000cwa.googlegroups.com>
Marco Minerva ha scritto:

> Hi all!
>
> Could anyone convert for me this piece of code, in C or Java?
>
> (let ((g (* 2 (or (gethash word good) 0)))
>       (b (or (gethash word bad) 0)))
>    (unless (< (+ g b) 5)
>      (max .01
>           (min .99 (float (/ (min 1 (/ b nbad))
>                              (+ (min 1 (/ g ngood))
>                                 (min 1 (/ b nbad)))))))))
>
> where "word" is a key for the hash tables "good" and "bad", "ngood" and
> "nbad" are two numbers.
>

Hi Marco,

what do you think about this:

{
  int g = good.getHash(word);
  int b = bad.getHash(word);

  if ( g == null ) {
    g = 0; }
  else {
    g = 2 * g; }

  if ( b == null ) {
    b = 0; }

  if (!((g + b) < 5)) {
    Math.max( 0.01,
              Math.min( 0.99, Math.min(1, b / nbad) /
                              (Math.min(1, g / ngood) +
                               Math.min(1, b / nbad)))); }
}

matteo

> Thanks in advance!
> -- 
> Marco Minerva, ·············@tiscali.it
From: Marco Minerva
Subject: Re: Convert a small piece of code
Date: 
Message-ID: <1132908584.640318.322900@g14g2000cwa.googlegroups.com>
Thanks a lot, you saved me!

Marco Minerva, ·············@tiscali.it
From: matteo d'addio 81
Subject: Re: Convert a small piece of code
Date: 
Message-ID: <1132915442.321378.26010@f14g2000cwb.googlegroups.com>
Marco Minerva ha scritto:

> Thanks a lot, you saved me!
>

Oops! It doesn't save nor return the result...

Try with:

  return Math.max( ...

matteo

> Marco Minerva, ·············@tiscali.it
From: Kaz Kylheku
Subject: Re: Convert a small piece of code
Date: 
Message-ID: <1133045878.618551.98820@g14g2000cwa.googlegroups.com>
matteo d'addio 81 wrote:
> what do you think about this:

>   if (!((g + b) < 5)) {
>     Math.max( 0.01,
>               Math.min( 0.99, Math.min(1, b / nbad) /
>                               (Math.min(1, g / ngood) +
>                                Math.min(1, b / nbad)))); }
> }

You transliterated the syntax, congratulations. But what about the
important matter of semantics?

What happens in Java when you divide b / nbad, where both are integers,
and b < nbad? What happens in Common Lisp?  Suppose b is 35 and nbad is
539. What is b / nbad?
From: Pascal Bourguignon
Subject: Re: Convert a small piece of code
Date: 
Message-ID: <87r793szcr.fsf@thalassa.informatimago.com>
"Kaz Kylheku" <········@gmail.com> writes:

> matteo d'addio 81 wrote:
>> what do you think about this:
>
>>   if (!((g + b) < 5)) {
>>     Math.max( 0.01,
>>               Math.min( 0.99, Math.min(1, b / nbad) /
>>                               (Math.min(1, g / ngood) +
>>                                Math.min(1, b / nbad)))); }
>> }
>
> You transliterated the syntax, congratulations. But what about the
> important matter of semantics?
>
> What happens in Java when you divide b / nbad, where both are integers,
> and b < nbad? What happens in Common Lisp?  Suppose b is 35 and nbad is
> 539. What is b / nbad?

Leave it!  Let him write loosy C software, the competition will be
easier on us...

-- 
"You question the worthiness of my code? I should kill you where you
stand!"
From: Michael Campbell
Subject: Re: Convert a small piece of code
Date: 
Message-ID: <uveyhn548.fsf@atlmicampbell.checkfree.com>
"Marco Minerva" <············@tiscali.it> writes:

> Hi all!
> 
> Could anyone convert for me this piece of code, in C or Java?
> 
> (let ((g (* 2 (or (gethash word good) 0)))
>       (b (or (gethash word bad) 0)))
>    (unless (< (+ g b) 5)
>      (max .01
>           (min .99 (float (/ (min 1 (/ b nbad))
>                              (+ (min 1 (/ g ngood))
>                                 (min 1 (/ b nbad)))))))))
> 

Is this from Paul Graham's spam algorithm?

-- 
Michael Campbell
From: Marco Minerva
Subject: Re: Convert a small piece of code
Date: 
Message-ID: <1132908480.363603.323380@g49g2000cwa.googlegroups.com>
Michael Campbell wrote:
> "Marco Minerva" <············@tiscali.it> writes:
>
> > Hi all!
> >
> > Could anyone convert for me this piece of code, in C or Java?
> >
> > (let ((g (* 2 (or (gethash word good) 0)))
> >       (b (or (gethash word bad) 0)))
> >    (unless (< (+ g b) 5)
> >      (max .01
> >           (min .99 (float (/ (min 1 (/ b nbad))
> >                              (+ (min 1 (/ g ngood))
> >                                 (min 1 (/ b nbad)))))))))
> >
>
> Is this from Paul Graham's spam algorithm?

Yes, that is...

Marco Minerva, ·············@tiscali.it
From: Harald Hanche-Olsen
Subject: Re: Convert a small piece of code
Date: 
Message-ID: <pcobr09u52l.fsf@shuttle.math.ntnu.no>
+ "Marco Minerva" <············@tiscali.it>:

| Could anyone convert for me this piece of code, in C or Java?

Yes, just about anyone could.

| Thanks in advance!

Don't mention it.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- Debating gives most of us much more psychological satisfaction
  than thinking does: but it deprives us of whatever chance there is
  of getting closer to the truth.  -- C.P. Snow
From: Kaz Kylheku
Subject: Re: Convert a small piece of code
Date: 
Message-ID: <1133031244.417701.270560@g44g2000cwa.googlegroups.com>
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.