Thread: [Flashforth-devel] For-next and do-loop practise
Brought to you by:
oh2aun
|
From: om1zz <om...@vo...> - 2011-06-16 13:36:41
|
Hi, in order not to invent wheel (and maybe quite useful for other ffters too) I would ask the practisioners for an advice how to best translate following constructs into FF's for-next: a) B A do ... loop b) B A do I ... loop c) D C do B A do ... I J ... loop loop d) B A do ... K +loop e) D C do B A do ... I J ... K +loop L +loop f) B A ?do ... loop Thanks, I. -- IHNED.cz je nový, přehlednější a rychlejší. Přesvědčte se na: www.ihned.cz |
|
From: Mikael N. <mik...@pp...> - 2011-06-16 14:59:08
|
Hi,
This is my untested quick take on this.
Of course if you are translating DO LOOP code
mechanically a direct template is good to have.
In general in FF I would not write code that
loops from A to B.
Usually I have a starting value A and a count N
Much code that uses do loops has the same and
must convert it to a A B=A+N pair before using the DO LOOP.
P is just a special variable which is saved on the return stack to
support nesting. J is implemented below without
nesting support. By saving it on the return stack
before FOR, it can also be made nestable.
-- Mike
On 16.6.2011 16:36, om1zz wrote:
> Hi, in order not to invent wheel (and maybe quite useful for other
> ffters too) I would ask the practisioners for an advice how to best
> translate following constructs into FF's for-next:
>
> a) B A do ... loop
B A - 1- for ... next
>
>
> b) B A do I ... loop
B A dup !p>r - 1- for @p p+ ... next r>p
>
>
> c) D C do
> B A do ... I J ... loop
> loop
variable j
D C dup !p>r - 1- for
B A dup j ! - 1- for
@p j @ ... p+ 1 j +!
next r>p
next
>
>
> d) B A do ... K +loop
B A - K / for ... next
>
>
> e) D C do
> B A do ... I J ... K +loop
> L +loop
variable j
D C dup !p>r - K / for
B A dup j ! L / for
@p j @ ... K p++ L j +!
next r>p
next
>
> f) B A ?do ... loop
B A - dup if 1- for ... next then
>
>
> Thanks,
> I.
>
>
|
|
From: om1zz <om...@vo...> - 2011-06-16 15:56:16
|
Mike, thanks! This should definately be put into you FF User Guide!! ?? d) e): K and L are arbitrary values which are added to the loop counter ?? not sure about the limits (the number of times it runs through the loop) a) B A do ... loop B A - 1- for ... next : testa 10 2 - 1- for 0 . next ; ok <#,ram> testa 0 0 0 0 0 0 0 ok <#,ram> b) B A do I ... loop B A dup !p>r - 1- for @p p+ ... next r>p : testb 10 2 dup !p>r - 1- for @p . p+ next r>p ; ok <#,ram> testb 2 3 4 5 6 7 8 ok <#,ram> c) D C do B A do ... I J ... loop loop variable j D C dup !p>r - 1- for B A dup j ! - 1- for @p j @ ... p+ 1 j +! next r>p next variable j : testc 10 2 dup !p>r - 1- for 10 2 dup j ! - 1- for @p j @ . . p+ 1 j +! next r>p next ; ok <#,ram> testc 2 2 3 3 4 4 5 5 6 6 7 7 8 8 ok <#,ram> it shall be probably: ------------------------ variable j : testc 10 2 dup j ! - 1- for 8 3 dup !p>r - 1- for @p j @ . . p+ next r>p 1 j +! next ; testc 2 3 2 4 2 5 2 6 3 3 3 4 3 5 3 6 4 3 4 4 4 5 4 6 5 3 5 4 5 5 5 6 6 3 6 4 6 5 6 6 7 3 7 4 7 5 7 6 8 3 8 4 8 5 8 6 ok <#,ram> d) B A do ... K +loop B A - K / for ... next Issue: Here K is usualy a step of any value (not (B-A/K)) e) D C do B A do ... I J ... K +loop L +loop variable j D C dup !p>r - K / for B A dup j ! L / for @p j @ ... K p++ L j +! next r>p next Issue: Here K and L are usualy steps of any value (not (B-A/K)) f) B A ?do ... loop B A - dup if 1- for ... next then : testf 10 2 - dup if 1- for 0 . next then ; ok <#,ram> testf 0 0 0 0 0 0 0 ok <#,ram> |
|
From: Mikael N. <mik...@pp...> - 2011-06-25 20:21:05
|
Hi, Some further thoughts: a) B A do ... loop The case a) cannot be translated. It will execute as f). f) B A ?do ... loop : testa #10 #2 - for r@ . next ; ok<$,ram> testa 7 6 5 4 3 2 1 0 ok<$,ram> b) This will also execute as ?do. B A ?do i . loop : testb #10 #2 dup !p>r - for @p . p+ next r>p ; ok<$,ram> testb 2 3 4 5 6 7 8 9 ok<$,ram> /Mike On 16.6.2011 18:56, om1zz wrote: > Mike, thanks! This should definately be put into you FF User Guide!! > > ?? d) e): K and L are arbitrary values which are added to the loop > counter > ?? not sure about the limits (the number of times it runs through > the loop) > > a) B A do ... loop > B A - 1- for ... next > > : testa 10 2 - 1- for 0 . next ; ok<#,ram> > testa 0 0 0 0 0 0 0 ok<#,ram> > > b) B A do I ... loop > B A dup !p>r - 1- for @p p+ ... next r>p > > : testb 10 2 dup !p>r - 1- for @p . p+ next r>p ; ok<#,ram> > testb 2 3 4 5 6 7 8 ok<#,ram> > > c) D C do > B A do ... I J ... loop > loop > > variable j > D C dup !p>r - 1- for > B A dup j ! - 1- for > @p j @ ... p+ 1 j +! > next r>p > next > > variable j > : testc > 10 2 dup !p>r - 1- for > 10 2 dup j ! - 1- for > @p j @ . . p+ 1 j +! > next r>p > next ; ok<#,ram> > testc 2 2 3 3 4 4 5 5 6 6 7 7 8 8 ok<#,ram> > > it shall be probably: > ------------------------ > variable j > : testc > 10 2 dup j ! - 1- for > 8 3 dup !p>r - 1- for > @p j @ . . p+ > next r>p > 1 j +! > next ; > > testc 2 3 2 4 2 5 2 6 3 3 3 4 3 5 3 6 4 3 4 4 4 5 4 > 6 5 3 5 4 5 5 5 6 6 3 6 4 6 5 6 6 7 3 7 4 7 5 7 6 8 3 > 8 4 8 5 8 6 ok<#,ram> > > > d) B A do ... K +loop > B A - K / for ... next > > Issue: Here K is usualy a step of any value (not (B-A/K)) > > e) D C do > B A do ... I J ... K +loop > L +loop > > variable j > D C dup !p>r - K / for > B A dup j ! L / for > @p j @ ... K p++ L j +! > next r>p > next > > Issue: Here K and L are usualy steps of any value (not (B-A/K)) > > f) B A ?do ... loop > B A - dup if 1- for ... next then > > : testf 10 2 - dup if 1- for 0 . next then ; ok<#,ram> > testf 0 0 0 0 0 0 0 ok<#,ram> > > |
|
From: om1zz <om...@vo...> - 2011-06-26 10:51:38
|
Mike, can the "do-loop" cases be summarized, thus we get a guide on it? I. > Some further thoughts: > > a) B A do ... loop > The case a) cannot be translated. It will execute > as f). > f) B A ?do ... loop > > : testa #10 #2 - for r@ . next ; ok<$,ram> > testa 7 6 5 4 3 2 1 0 ok<$,ram> > > b) This will also execute as ?do. > B A ?do i . loop > > : testb #10 #2 dup !p>r - for @p . p+ next r>p ; > ok<$,ram> > testb 2 3 4 5 6 7 8 9 ok<$,ram> > > /Mike -- IHNED.cz je nový, přehlednější a rychlejší. Přesvědčte se na: www.ihned.cz |