Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #26007
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: Starting Forth 6. |
| Date | 2013-09-24 23:47 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <l1t89p$gbu$1@dont-email.me> (permalink) |
| References | <l1t641$74t$1@dont-email.me> <l1t7mq$ds6$1@dont-email.me> |
WJ wrote:
> 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
>
> Modified so that when the closure is called with no arguments,
> the entire vector is returned.
>
> (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) ]
> [() the-vec])))
>
>
> > (define board (array 4 4))
> > (for* ([row 4] [col 4])
> (board row col (+ col (* row 10))))
> > (board)
> '#(0 1 2 3 10 11 12 13 20 21 22 23 30 31 32 33)
Modified so that when the closure is called with one argument,
a row of the vector is returned.
(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) ]
[() the-vec]
[(row)
(vector-copy the-vec
(calc-index row 0) (calc-index (add1 row) 0))])))
> (define board (array 4 4))
> (for* ([row 4] [col 4])
(board row col (+ col (* row 10))))
> (board 3)
'#(30 31 32 33)
Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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