gforth
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gforth] my vsum function is suspiciously non-local


From: Terrence Brannon
Subject: [gforth] my vsum function is suspiciously non-local
Date: Sun, 7 Mar 2010 12:53:05 -0500

I used a "global variable" named tally to handle this tutorial
assignment, but I think there must be a way to avoid the use of such a
variable in solving this problem. Any help is appreciated:

\    Assignment: Write a definition vsum ( addr u -- n ) that computes
the sum of u cells, with the first of these cells at addr, the next
one at addr cell+ etc.

( Usage:
     create vx 2 , 2 , 2 , 2 , 2 ,
     vx 5 vsum
)

: loop-range ( number-of-cells -- number-of-cells zero )  0 ;
: get-value-at-address-offset ( address offset -- v ) cells + @ ;
: increase-value ( n addr -- v+n ) TUCK @ + SWAP ! ;
VARIABLE tally
: vsum { address u }
    u loop-range U+DO
        address i get-value-at-address-offset
        tally increase-value
    LOOP
    tally @ ;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]