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


Groups > comp.lang.forth > #26417

Re: Starting Forth 6.

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Re: Starting Forth 6.
Date 2013-10-12 09:17 +0000
Organization A noiseless patient Spider
Message-ID <l3b43m$7db$1@dont-email.me> (permalink)
References <l1t641$74t$1@dont-email.me>

Show all headers | View raw


WJ wrote:

> Brodie creates a defining word ARRAY, which creates a
> two-dimensional array.  However, the words created by
> ARRAY don't fetch values from the array or store values
> in the array; they only return the addresses of array
> elements.
> 
> Racket:
> 
> The following function returns a closure:
> 
> (define (array rows cols)
>   (let ((the-vec (make-vector (* rows cols) 0)))
>     (define (calc-index row col) (+ (* row cols) col))
>     (case-lambda
>       [(row col) (vector-ref the-vec (calc-index row col))]
>       [(row col new-val)
>        (vector-set! the-vec (calc-index row col) new-val)])))
> 
> (define board (array 4 4))
> 
> When called with 3 arguments, the third is placed in the array;
> when called with 2 arguments, the value in the cell is returned.
> 
>   > (board 0 0 999)
>   > (board 0 0)
>   999
> 
> (for* ([row 4] [col 4])
>   (board row col (+ col (* row 10))))
> (for ([row 4])
>   (for ([col 4])
>     (display (~a (board row col) #:width 3 #:align 'right)))
>   (newline))
> 
>   0  1  2  3
>  10 11 12 13
>  20 21 22 23
>  30 31 32 33

Here's a version in Ruby.

def matrix rows, cols
  the_vector = [0] * (rows * cols)
  calc_index = proc{|row,col| row*cols + col}
  proc{|row,col,*val|
    if val.empty?
      the_vector[ calc_index[row,col] ]
    else
      the_vector[ calc_index[row,col] ] = *val
    end
  }
end

mat = matrix( 4, 4 )
4.times{|row| 4.times{|col| mat[row,col, row*col]}}

4.times{|row|
  4.times{|col| printf "%2d", mat[row,col] }
  puts
}

 0 0 0 0
 0 1 2 3
 0 2 4 6
 0 3 6 9

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


Thread

Starting Forth 6. "WJ" <w_a_x_man@yahoo.com> - 2013-09-24 23:09 +0000
  Re: Starting Forth 6. "WJ" <w_a_x_man@yahoo.com> - 2013-09-24 23:36 +0000
    Re: Starting Forth 6. "WJ" <w_a_x_man@yahoo.com> - 2013-09-24 23:47 +0000
  Re: Starting Forth 6. "Ed" <invalid@invalid.com> - 2013-09-25 18:22 +1000
    Re: Starting Forth 6. "Elizabeth D. Rather" <erather@forth.com> - 2013-09-25 08:44 -1000
      Re: Starting Forth 6. hughaguilar96@yahoo.com - 2013-09-25 20:09 -0700
        Re: Starting Forth 6. albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-09-26 12:27 +0000
          Re: Starting Forth 6. "Elizabeth D. Rather" <erather@forth.com> - 2013-09-26 09:09 -1000
            Re: Starting Forth 6. hughaguilar96@yahoo.com - 2013-09-27 23:16 -0700
              Re: Starting Forth 6. the_gavino_himself <visphatesjava@gmail.com> - 2013-11-16 17:07 -0800
            Re: Starting Forth 6. "Clyde W. Phillips Jr." <cwpjr02@gmail.com> - 2013-10-11 22:37 -0700
              Re: Starting Forth 6. the_gavino_himself <visphatesjava@gmail.com> - 2013-11-16 17:11 -0800
                Re: Starting Forth 6. "Stanley Daniel de Liver" <notagoodone@invalid.org.invalid> - 2013-12-11 16:49 +0000
          Re: Starting Forth 6. "Ed" <invalid@invalid.com> - 2013-09-27 11:39 +1000
            Re: Starting Forth 6. "Clyde W. Phillips Jr." <cwpjr02@gmail.com> - 2013-10-11 22:39 -0700
          Re: Starting Forth 6. hughaguilar96@yahoo.com - 2013-09-30 16:47 -0700
          Re: Starting Forth 6. David Thompson <dave.thompson2@verizon.net> - 2013-10-12 18:22 -0400
            Re: Starting Forth 6. albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-10-13 12:17 +0000
        Re: Starting Forth 6. Whammo <sidciavic@gmail.com> - 2013-09-26 11:37 -0700
          Re: Starting Forth 6. hughaguilar96@yahoo.com - 2013-09-27 22:28 -0700
          Re: Starting Forth 6. "Clyde W. Phillips Jr." <cwpjr02@gmail.com> - 2013-10-11 22:34 -0700
    Re: Starting Forth 6. hughaguilar96@yahoo.com - 2013-09-25 19:58 -0700
  Re: Starting Forth 6. "WJ" <w_a_x_man@yahoo.com> - 2013-10-01 15:21 +0000
  Re: Starting Forth 6. "WJ" <w_a_x_man@yahoo.com> - 2013-10-12 09:17 +0000
  Re: Starting Forth 6. "WJ" <w_a_x_man@yahoo.com> - 2013-10-12 19:01 +0000

csiph-web