Re: The Promise of Forth



"Mark W. Humphries" <mwh@xxxxxxxxxxxxxxx> writes:

On Mar 27, 6:28 am, John Doty <j...@xxxxxxxxxxxxxxxxxxxxxxx> wrote:
Stan Katz wrote:
John Doty <j...@xxxxxxxxxxxxxxxxxxxxxxx> wrote in
news:tbCdnc4VfuX_J3TanZ2dnUVZ_ryqnZ2d@xxxxxxxxxxxxx:

it's comprehensible. Consider:

def lmedian (inlist,numbins=1000):
"""
Returns the computed median value of a list of numbers, given the
number of bins to use for the histogram (more bins brings the computed value
closer to the median score, default number of bins = 1000). See G.W.
Heiman's Basic Stats (1st Edition), or CRC Probability & Statistics.

Usage: lmedian (inlist, numbins=1000)
"""
(hist, smallest, binsize, extras) = histogram(inlist,numbins) # make histog
cumhist = cumsum(hist) # make cumulative histogram
for i in range(len(cumhist)): # get 1st(!) index holding 50%ile score
if cumhist[i]>=len(inlist)/2.0:
cfbin = i
break
LRL = smallest + binsize*cfbin # get lower read limit of that bin
cfbelow = cumhist[cfbin-1]
freq = float(hist[cfbin]) # frequency IN the 50%ile bin
median = LRL + ((len(inlist)/2.0 - cfbelow)/float(freq))*binsize # median formula
return median

This is certainly a scientific statistician's notion of a code for this
problem, not a programmer's (the author is the Director of Neural
Systems Group at Massachusetts General Hospital). And that's good: it's
intended to be usable and readable by a scientist or statistician.

Note that in some ways the comments aren't all that great: for example,
nowhere does it tell me that the result is an estimate based on
interpolation within a histogram bin, but the "median formula" suggests
that, and reading backward confirms it. The code speaks for itself.
Indeed, I think it's easiest to read this code backward from the result,
filling in the mental details of how it gets there as you go. Syntax is
essential to this process.

But in Forth, there's:

\ find.seq ACM Algorithm #65 (iterative implementation)

\ Forth Scientific Library Algorithm #26

\ }find ( n &p k -- k )
\ Find will assign to the floating point array element p[k],
\ the value which it would have if the floating point array
\ p[0..n-1] was sorted (downward).
\ Useful for finding the median or similar elements of an array.
\ Requires 1/3 N log(N) exchanges on the average

\ (P will be partially sorted in the process, and subsequent
\ calls to }find will run faster).

\ This is an ANS Forth program requiring:
\ 1. The Floating-Point word set
\ 2. The word '}' to dereference a one-dimensional array (ARRAY).
\ 3. Uses the words 'FLOAT' and ARRAY to create floating point arrays.
\ 4. Uses the words 'DARRAY' and '&!' to set array pointers.
\ 5. Uses words 'Private:', 'Public:' and 'Reset_Search_Order'
\ to control visibility of internal code
\ 6. The test code uses the immediate word '%' which takes the
\ next token and converts it to a floating-point literal
\ 7. The word '}fprint' is needed to print a one dimensional
\ floating point array in the test code.
\ 8. The compilation of the test code is controlled by VALUE TEST-CODE?
\ and the conditional compilation words in the Programming-Tools
\ wordset.
\

\ Collected Algorithms from ACM, Volume 1 Algorithms 1-220,
\ 1980; Association for Computing Machinery Inc., New York,
\ ISBN 0-89791-017-6

\ (c) Copyright 1994 Everett F. Carter. Permission is granted by the
\ author to use this software for any application provided this
\ copyright notice is preserved.

CR .( FIND V1.0 13 November 1994 EFC )

Private:

FLOAT DARRAY p{ \ pointer to user array

: inc-i ( i -- i' ) ( F: x -- x )
BEGIN
FDUP p{ OVER } F@ F<
WHILE
1+
REPEAT
;

: dec-j ( j -- j' ) ( F: x -- x )
BEGIN
p{ OVER } F@ FOVER F<
WHILE
1-
REPEAT
;

: exchange ( i j -- )
p{ SWAP } DUP F@
p{ ROT } DUP F@
SWAP
F! F!
;

: replace-l? ( k l r i j -- k l' r i j ) \ conditionally replace l with i
DUP 5 PICK < IF 2>R SWAP DROP R@ SWAP 2R> THEN
;

: replace-r? ( k l r i j -- k l r' ) \ conditionally replace r with j
\ and remove excess values
4 PICK ROT < IF SWAP THEN DROP
;

Public:

: }find ( n &p k -- k )

SWAP & p{ &!

SWAP 1- 0 SWAP ( k l=0 r=n-1 )

BEGIN
2DUP <
WHILE
p{ 3 PICK } F@
2DUP ( k l r i=l j=r )

BEGIN
SWAP inc-i
SWAP dec-j
2DUP > 0= IF 2DUP exchange SWAP 1+ SWAP 1- THEN
2DUP >
UNTIL

FDROP
replace-l?
replace-r?
REPEAT

2DROP
;

Ugh.

The code is *dominated* by stack gymnastics, not by an exposition of the
algorithm.

The code itself is poorly commented, and most of those comments describe
stack contents rather that what's going on algorithmically and why.

The comments preceding the code are dominated by an exposition of the
code's dependencies, an issue the Python user can largely ignore.

Forth's lack of syntax forces the reader to work forward through the
code, consciously tracking the stack, rather than finding the strategic
center of the algorithm and working out the data flows to and from it.

The author of this horrible example apparently has a very high
reputation in the Forth community. If someone of his stature cannot
create readable Forth code, who can?

I assert that few programmers, even Forth users who have never seen
Python, would find the Forth version more readable. And I doubt there is
a single statistician in the world who would find the Forth version more
readable.

I find it ironic that you continuously criticize Forth's syntax when
one of Forth's unique features is that it imposes no restrictions on
syntax and it allows one the complete freedom to extend and morph the
language to suit one's needs and biases. With Forth you can have it
your way, so why complain? Just do it.

Why didn't the author of Forth code above do it? This is the point:
Forth expert failed to provide valuable example of feature usage,
thus this "unique feature" is harmful, not useful.

You have a very negative way of pushing your agenda, all you do is
harp about what you don't like instead of specifying and building what
you want.

Why didn't you do it for John then? Just do it to prove John is wrong.

The Forth community has a long history of Forthers implementing their
vision of Forth, starting with Chuck Moore. Look at Gforth, Holon,
iForth, Pygmy

Gforth is dead, Pygmy is dead for even longer time, Holon...
What is it? The only meaning Google reveals is the name of town.

What useful code is written for HolonForth?

various object oriented Forths, experimental Forths of
all kinds. None of these people have found it necessary to convince
the rest of the Forth community that their vision of Forth was right
and everyone else's was wrong as a prerequisite to implementing their
vision.

What did they write except yet another experimental Forth?


--
BECHA...
CKOPO CE3OH...
.



Relevant Pages

  • Re: The Promise of Forth
    ... Returns the computed median value of a list of numbers, ... number of bins to use for the histogram (more bins brings the computed value ...
    (comp.lang.forth)
  • Re: The Promise of Forth
    ... Returns the computed median value of a list of numbers, ... given the number of bins to use for the histogram (more bins ...
    (comp.lang.forth)
  • Re: computing on streams of data
    ... you need to be able to keep a running median for the first N values? ... We wanted to be able to show the distribution of values ... What I ended up doing was creating bins that were logarithmically ... a K-bit approximation using significantly fewer bits than for the ...
    (comp.theory)
  • Re: Histogram equalization
    ... There is nothing in MATLAB that does accurate histogram matching. ... transform it accurately to the histogram of the second image. ... certain bins with other bins completely empty. ... get a very flat histogram and ALL the bins will be filled up. ...
    (comp.soft-sys.matlab)
  • Re: The Promise of Forth
    ... Returns the computed median value of a list of numbers, ... number of bins to use for the histogram (more bins brings the computed value ... closer to the median score, default number of bins = 1000). ...
    (comp.lang.forth)