On Mar 12, 11:29 am, m...@[EMAIL PROTECTED]
(Marcel Hendrix) wrote:
> William James <w_a_x_...@[EMAIL PROTECTED]
> writes Re: BETWEEN
> [..]
>
> >> Playing with the algorithm for WITHIN (A.6.2.2440) one gets:
>
> >> : BETWEEN ( n1|u1 n2|u2 n3|u3 -- flag ) OVER - -ROT - U< 0= ;
> > Stuff like this leads one to believe that Forth code is
> > unreadable, cryptic, unsharable, and unmaintainable.
> > It's worse than some assembly language I've seen.
> > Forth needs to become a higher-level language.
>
> Maybe, but in the case under investigation the offered
> functionality might be trickier than a quick glance
> reveals. Maybe you can rewrite BETWEEN in Pascal to find
> out just how tricky.
>
> -marcel
Yes, the behavior of the function is tricky;
it's hard to predict after looking at the code what
the output would be for a given set of inputs.
Is this the kind of code that we want?
As an exercise and a demonstration, I did code it
in Pascal. Since the language is very type-aware,
two functions are used: one for signed integers and
one for unsigned. Because of the bizarre algorithm,
the behavior of the Pascal code is almost as hard to
predict as that of the Forth code.
// for unsigned 32-bit numbers
function between( x, low, high: cardinal ): boolean;
begin
write( ' unsigned ' );
exit( not ( cardinal(high - low) < cardinal(x - low)) )
end;
// for signed 32-bit numbers
function between( x, low, high: longint ): boolean;
begin
write( ' signed ' );
exit( between( cardinal(x), cardinal(low), cardinal(high) ) )
end;
var i: longint;
begin
for i := -1 to 5 do
writeln( i, ' ', between( i, 2, 4 ) );
for i := -1 to 5 do
writeln( cardinal(i), ' ',
between( cardinal(i), cardinal(2), cardinal(4) ) );
for i := -1 to 5 do
writeln( i, ' ', between( i, 4, 2 ) )
end.
--- output ---
-1 signed unsigned FALSE
0 signed unsigned FALSE
1 signed unsigned FALSE
2 signed unsigned TRUE
3 signed unsigned TRUE
4 signed unsigned TRUE
5 signed unsigned FALSE
4294967295 unsigned FALSE
0 unsigned FALSE
1 unsigned FALSE
2 unsigned TRUE
3 unsigned TRUE
4 unsigned TRUE
5 unsigned FALSE
-1 signed unsigned TRUE
0 signed unsigned TRUE
1 signed unsigned TRUE
2 signed unsigned TRUE
3 signed unsigned FALSE
4 signed unsigned TRUE
5 signed unsigned TRUE


|