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


Groups > comp.lang.forth > #59639 > unrolled thread

Rosetta: Loops/Nested

Started by"Robert L." <No_spamming@noWhere_7073.org>
First post2017-05-21 09:43 +0000
Last post2017-05-21 17:47 +0200
Articles 4 — 3 participants

Back to article view | Back to comp.lang.forth


Contents

  Rosetta: Loops/Nested "Robert L." <No_spamming@noWhere_7073.org> - 2017-05-21 09:43 +0000
    Re: Rosetta: Loops/Nested Bruno Gauthier <bgauthier@free.fr> - 2017-05-21 17:13 +0200
      Re: Rosetta: Loops/Nested Julian Fondren <julian.fondren@gmail.com> - 2017-05-21 08:29 -0700
        Re: Rosetta: Loops/Nested Bruno Gauthier <bgauthier@free.fr> - 2017-05-21 17:47 +0200

#59639 — Rosetta: Loops/Nested

From"Robert L." <No_spamming@noWhere_7073.org>
Date2017-05-21 09:43 +0000
SubjectRosetta: Loops/Nested
Message-ID<ofrnfl$11ad$1@gioia.aioe.org>
> Show a nested loop which searches a two-dimensional array
> filled with random numbers uniformly distributed over
> [1 , ... , 20].
> 
> The loops iterate rows and columns of the array printing the
> elements until the value 20 is met.
> 
> Specifically, this task also shows how to break out of nested loops.


(use gauche.array)
(use gauche.generator)
(use srfi-27)

(define (array->generator ary)
  (generate
    (lambda (yield)
      (array-for-each-index ary
        (lambda(i j) (yield (array-ref ary i j)))))))

(define data
  (tabulate-array (shape 0 10 0 10) (lambda _ (random-integer 30))))

(generator-find
  (lambda (x) (format #t "~a " x) (= 20 x))
  (array->generator data))

 ===>
4 27 23 13 9 25 15 18 4 3 18 14 7 10 19 15 20 20

In Forth?

-- 
The struggle of our time is to concentrate, not to dissipate: to renew our
association with traditional wisdom: to re-establish a vital connection between
the individual and the race.  It is, in a word, a struggle against Liberalism.
 --- T. S. Elliot

[toc] | [next] | [standalone]


#59657

FromBruno Gauthier <bgauthier@free.fr>
Date2017-05-21 17:13 +0200
Message-ID<5921ae31$0$31620$426a34cc@news.free.fr>
In reply to#59639
Le 21/05/2017 à 11:43, Robert L. a écrit :
>> Show a nested loop which searches a two-dimensional array
>> filled with random numbers uniformly distributed over
>> [1 , ... , 20].
>>
>> The loops iterate rows and columns of the array printing the
>> elements until the value 20 is met.
>>
>> Specifically, this task also shows how to break out of nested loops.
>
>
> (use gauche.array)
> (use gauche.generator)
> (use srfi-27)
>
> (define (array->generator ary)
>   (generate
>     (lambda (yield)
>       (array-for-each-index ary
>         (lambda(i j) (yield (array-ref ary i j)))))))
>
> (define data
>   (tabulate-array (shape 0 10 0 10) (lambda _ (random-integer 30))))
>
> (generator-find
>   (lambda (x) (format #t "~a " x) (= 20 x))
>   (array->generator data))
>
>  ===>
> 4 27 23 13 9 25 15 18 4 3 18 14 7 10 19 15 20 20
>
> In Forth?
>
Hi robert,
why two 20 at the end ? it should stop at the first not?
why ( random-interger 30 ) ? [1, ..., 20 ] dont indicate between 1 and 
20, 20 included ?

what happens if the random function dont generate a 20 number in the 
array ?

here a forth version :
\ nestedloops
10 10 * buffer: zone1
false value finish?
\ To be sure that the numbers are correctly distributed and
\ each one in the array, i fill the array five times with
\ numbers from 1 to 20 and then mix the array.
\ if we dont do that they are a probability, that 20 is not
\ in the array.

: initialisations ( -- )
   false to finish?
   5 0 do 20 0 do i 1+ zone1 j 20 * + i + c! loop loop
   1000 0 do zone1 100 random +
             zone1 100 random +
             over c@ over c@ swap rot c! swap c!
          loop
;
\ first version of nestedloops with begin until loops
: nestedloops ( -- )
   cr

   10 0 begin cr
   10 0 begin 2over nip 10 * zone1 + over + c@ dup . 20 = to finish?
              1+ 2dup = finish? or
        until 2drop

              1+ 2dup = finish? or
        until 2drop


;
\ second version of nestedloops with do loop loops
: nestedloops2
   cr

   10 0 do cr
   10 0 do zone1 j 10 * + i + c@ dup . 20 = to finish?
           finish? ?leave
        loop
           finish? ?leave
        loop


;


: test initialisations 2 random cr
   if ." 1st version with begin until loops : " nestedloops
   else ." 2nd version with do loop loops : "   nestedloops2
   then ;

[toc] | [prev] | [next] | [standalone]


#59660

FromJulian Fondren <julian.fondren@gmail.com>
Date2017-05-21 08:29 -0700
Message-ID<fe348c7d-1940-4000-95ce-f1fed2327606@googlegroups.com>
In reply to#59657
On Sunday, May 21, 2017 at 10:11:46 AM UTC-5, Bruno Gauthier wrote:
> \ first version of nestedloops with begin until loops
> : nestedloops ( -- )
>    cr
> 
>    10 0 begin cr
>    10 0 begin 2over nip 10 * zone1 + over + c@ dup . 20 = to finish?
>               1+ 2dup = finish? or
>         until 2drop
> 
>               1+ 2dup = finish? or
>         until 2drop
> 
> 
> ;
> \ second version of nestedloops with do loop loops
> : nestedloops2
>    cr
> 
>    10 0 do cr
>    10 0 do zone1 j 10 * + i + c@ dup . 20 = to finish?
>            finish? ?leave
>         loop
>            finish? ?leave
>         loop
> 
> 
> ;
> 

  : nested ( -- )
    zone1 10 10 * bounds do
      cr i 10 bounds do
        i c@ dup .
        20 = if unloop unloop exit then
      loop
    10 +loop ;

Although there's not really any reason for the inner loop.

[toc] | [prev] | [next] | [standalone]


#59663

FromBruno Gauthier <bgauthier@free.fr>
Date2017-05-21 17:47 +0200
Message-ID<5921b649$0$8918$426a34cc@news.free.fr>
In reply to#59660
Le 21/05/2017 à 17:29, Julian Fondren a écrit :
> On Sunday, May 21, 2017 at 10:11:46 AM UTC-5, Bruno Gauthier wrote:
>> \ first version of nestedloops with begin until loops
>> : nestedloops ( -- )
>>    cr
>>
>>    10 0 begin cr
>>    10 0 begin 2over nip 10 * zone1 + over + c@ dup . 20 = to finish?
>>               1+ 2dup = finish? or
>>         until 2drop
>>
>>               1+ 2dup = finish? or
>>         until 2drop
>>
>>
>> ;
>> \ second version of nestedloops with do loop loops
>> : nestedloops2
>>    cr
>>
>>    10 0 do cr
>>    10 0 do zone1 j 10 * + i + c@ dup . 20 = to finish?
>>            finish? ?leave
>>         loop
>>            finish? ?leave
>>         loop
>>
>>
>> ;
>>
>
>   : nested ( -- )
>     zone1 10 10 * bounds do
>       cr i 10 bounds do
>         i c@ dup .
>         20 = if unloop unloop exit then
>       loop
>     10 +loop ;
>
> Although there's not really any reason for the inner loop.
>
true
but probably, he want that we show a solution to finish nested loops
it's done for me as for you
bruno

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web