Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #25981

Starting Forth 5.

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Starting Forth 5.
Date 2013-09-23 21:41 +0000
Organization A noiseless patient Spider
Message-ID <l1qcit$998$1@dont-email.me> (permalink)

Show all headers | View raw


: DOUBLED
  6 1000 21 1 DO CR ." YEAR " I 2 U.R
  2DUP R% + DUP ." BALANCE " .
  DUP 2000 > IF CR CR ." more than doubled in "
  I . ." years " LEAVE
  THEN
  LOOP 2DROP ;

The result will look like this:

DOUBLED
YEAR 1 BALANCE 1060
YEAR 2 BALANCE 1124
YEAR 3 BALANCE 1191
YEAR 4 BALANCE 1262
YEAR 5 BALANCE 1338
YEAR 6 BALANCE 1418
YEAR 7 BALANCE 1503
YEAR 8 BALANCE 1593
YEAR 9 BALANCE 1609
YEAR 10 BALANCE 1790
YEAR 11 BALANCE 1897
YEAR 12 BALANCE 2011
more than doubled in 12 years ok


As usual, the code is undecipherable; all you can see
is stack juggling.  Would you pay anyone to create
unmaintainable code like that?

And the results are inaccurate, since integers are
used instead of floating point.

And there is a typo for year 9; the amount shoud be
1689.

If you want it done correctly, a modern high-level
language like Ruby must be used.

def doubled()
  rate = 0.06
  principle = original = 1000
  year = 0
  begin
    year += 1
    principle *= 1 + rate
    printf "Year %2d  Balance %7.2f\n", year, principle
  end until principle > 2 * original
  puts "More than doubled in #{ year } years."
end

doubled
Year  1  Balance 1060.00
Year  2  Balance 1123.60
Year  3  Balance 1191.02
Year  4  Balance 1262.48
Year  5  Balance 1338.23
Year  6  Balance 1418.52
Year  7  Balance 1503.63
Year  8  Balance 1593.85
Year  9  Balance 1689.48
Year 10  Balance 1790.85
Year 11  Balance 1898.30
Year 12  Balance 2012.20
More than doubled in 12 years.

This is a very dull, boring, pedestrian, and elementary
problem for any normal programmer.  To a Forth worshipper,
it's intensely exciting and challenging, since he is using
a stone-age language that is suited only for programming
an embedded controller that flushes a toilet.

That this crude book is considered a classic by Forth
programmers tells us all that we need to know about them.

Back to comp.lang.forth | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Starting Forth 5. "WJ" <w_a_x_man@yahoo.com> - 2013-09-23 21:41 +0000
  Re: Starting Forth 5. Doug Hoffman <glidedog@gmail.com> - 2013-09-24 07:31 -0400
    Re: Starting Forth 5. mhx@iae.nl - 2013-09-24 10:29 -0700
    Re: Starting Forth 5. rickman <gnuarm@gmail.com> - 2013-09-24 21:07 -0400
      Re: Starting Forth 5. mhx@iae.nl - 2013-09-24 21:33 -0700
        Re: Starting Forth 5. albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-09-25 09:20 +0000
      Re: Starting Forth 5. "Rod Pemberton" <dont_use_email@nohavenotit.com> - 2013-09-25 02:41 -0400
      Re: Starting Forth 5. albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-09-25 09:15 +0000
        Re: Starting Forth 5. rickman <gnuarm@gmail.com> - 2013-09-25 16:35 -0400
  Re: Starting Forth 5. albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-09-24 18:21 +0000
    Re: Starting Forth 5. "Alex McDonald" <blog@rivadpm.com> - 2013-09-24 19:43 +0100

csiph-web