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


Groups > comp.lang.forth > #29061

+DO and programming without stack manipulation words

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject +DO and programming without stack manipulation words
Date 2014-03-18 13:41 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2014Mar18.144137@mips.complang.tuwien.ac.at> (permalink)

Show all headers | View raw


This year I use Forth in the first part of our introductory
programming course beginners.  You can find the course notes (in
German) on

<http://www.complang.tuwien.ac.at/anton/lvas/pk/>

I don't teach them full Forth.  In particular, I don't teach them
stack manipulation (apart from DROP).  Instead, I use locals in a
static-single-assignment way.  That leads to interesting code such as

: step { xn -- xn1 }
  xn 2 mod 0 = if
    xn 2 /
  else
    xn 3 * 1 +
  endif ;

: steps ( x0 k -- )
  0 ?do { xn }
    xn .
    xn step
  loop drop ;

Locals definitions inside control structures (and sometimes several
locals definitions) are necessary given the restrictions I use.

One disadvantage of using locals (at all) is that I cannot write
traces of execution that can be executed directly.

When I started out, I wanted to make the course not so
Gforth-specific, so I taught them ?DO...LOOP instead of +DO...LOOP and
U+DO...LOOP.  On at least the FAC example this leads to less efficient
code.  A solution that works for n>=0 looks like this with ?DO:

: fac { n -- n! }
  1
  n 1 + 1 ?do ( n1 ) \ n1 = 1*1*2*..*(i-1)=(i-1)!
    i *       ( n2 ) \ n2 = 1*1*2*..*i    = i!
  loop ;

With +DO we can use 

: fac { n -- n! }
  1
  n 1 + 2 +do ( n1 ) \ n1 = 1*2*..*(i-1)=(i-1)!
    i *       ( n2 ) \ n2 = 1*2*..*i    = i!
  loop ;

which is comes out straightforward from my explanation and performs
one iteration less (and therefore is probably more efficient).

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2013: http://www.euroforth.org/ef13/

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


Thread

+DO and programming without stack manipulation words anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-03-18 13:41 +0000
  Re: +DO and programming without stack manipulation words Bernd Paysan <bernd.paysan@gmx.de> - 2014-03-18 17:30 +0100
    Re: +DO and programming without stack manipulation words anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-03-18 16:50 +0000
      Re: +DO and programming without stack manipulation words Bernd Paysan <bernd.paysan@gmx.de> - 2014-03-18 18:56 +0100
        Re: +DO and programming without stack manipulation words anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-03-18 18:25 +0000
          Re: +DO and programming without stack manipulation words Bernd Paysan <bernd.paysan@gmx.de> - 2014-03-18 22:21 +0100
            Re: +DO and programming without stack manipulation words albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-03-19 11:42 +0000
              Re: +DO and programming without stack manipulation words Bernd Paysan <bernd.paysan@gmx.de> - 2014-03-19 14:59 +0100
                Re: +DO and programming without stack manipulation words Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-03-20 02:13 -0500
                Re: +DO and programming without stack manipulation words albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-03-20 12:50 +0000
            Re: +DO and programming without stack manipulation words Alexander Skobelev <al.skobelev@gmail.com> - 2014-03-19 20:47 -0700
            Re: +DO and programming without stack manipulation words Alexander Skobelev <al.skobelev@gmail.com> - 2014-03-19 21:24 -0700
            Re: +DO and programming without stack manipulation words Alexander Skobelev <al.skobelev@gmail.com> - 2014-03-19 23:35 -0700
          Re: +DO and programming without stack manipulation words Mark Wills <markwills1970@gmail.com> - 2014-03-20 01:27 -0700
        Re: +DO and programming without stack manipulation words Paul Rubin <no.email@nospam.invalid> - 2014-03-18 12:24 -0700
    Re: +DO and programming without stack manipulation words albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-03-18 19:23 +0000
    Re: +DO and programming without stack manipulation words Assad Ebrahim <assad.ebrahim@alum.swarthmore.edu> - 2014-03-19 18:36 +0000
      Re: +DO and programming without stack manipulation words Spam@ControlQ.com - 2014-03-19 17:32 -0400
        Re: +DO and programming without stack manipulation words Bernd Paysan <bernd.paysan@gmx.de> - 2014-03-19 23:48 +0100
      Re: +DO and programming without stack manipulation words Bernd Paysan <bernd.paysan@gmx.de> - 2014-03-19 23:17 +0100
        Re: +DO and programming without stack manipulation words Assad Ebrahim <assad.ebrahim@alum.swarthmore.edu> - 2014-03-20 01:47 +0000
          Re: +DO and programming without stack manipulation words Bernd Paysan <bernd.paysan@gmx.de> - 2014-03-20 23:00 +0100
      Re: +DO and programming without stack manipulation words albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-03-20 13:00 +0000

csiph-web