SELECT (was: Back & Forth - Co-routines)

Liste des GroupesRevenir à cl forth 
Sujet : SELECT (was: Back & Forth - Co-routines)
De : anton (at) *nospam* mips.complang.tuwien.ac.at (Anton Ertl)
Groupes : comp.lang.forth
Date : 01. Feb 2025, 09:54:41
Autres entêtes
Organisation : Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID : <2025Feb1.095441@mips.complang.tuwien.ac.at>
References : 1 2 3 4 5
User-Agent : xrn 10.11
melahi_ahmed@yahoo.fr (ahmed) writes:
On Sat, 1 Feb 2025 7:26:11 +0000, Anton Ertl wrote:
[undefined] select [if]
: select ( u1 u2 f -- u )
  if swap then nip ;
[then]
>
: tri_mf3.1 { x a b c -- mf }
    x a c within if
      b x  x b < a c select  blend
    else
      0
    then ;
...
I think it must be:
(the true/false flag must on tos for select as you defined it)
>
: tri_mf3.1 { x a b c -- mf }
   x a c within if
     b x a c x b < select  blend
   else
     0
   then ;

Yes, thank you.  Why did I make this bug?  I originally wrote

x b < if a else c then

then replaced the IF ELSE THEN with SELECT, but did not change the
order of arguments accordingly.

It's an interesting question of whether the ordering of arguments in
SELECT is optimal.  Let's look at the existing uses of SELECT in
Gforth (only one line given, this comes from WHERE):

Flag last preferable:
cell [ 2 cells ] Literal I cell- 2@ <> select \ skip double entries             
0 [: over defers addr>view dup 0= select ;] sections-execute nip ;                  
i dup >cfa swap name>string drop cell negate and dup 0= select                          
2>r $substitute over r@ u<= -78 swap select -rot                                 
dup name>compile >r swap name>interpret state @ select                                

Flag first preferable:
dup 0< 'A' 'B' rot select  hold abs #n #esc[                                           
dup 0< 'D' 'C' rot select  hold abs #n #esc[                                           
dup $80 $20 vt100-modifier @ 2 and select                                           
cells altkeys ctrlkeys vt100-modifier @ 2 and select                                
: .s/d ( -- ) 's' 'd' s? IF ss? ELSE length @ 0= THEN select emit ;       
: .d/s ( -- ) 'd' 's' s? IF ss? ELSE length @ 0= THEN select emit ;       

Does not matter:
replace-sourceview current-sourceview over select ;                             
dup 7 <= IF  'r' 'e' w? select emit  ELSE  'r' emit  THEN  ;              
"breg2 "breg p? select  ELSE  "wregs ( " ) THEN  THEN                     
: .s/p ( -- ) 's' 'p' s? select emit ;                                    
: .p/s ( -- ) 'p' 's' s? select emit ;                                    
: .ssereg ( n -- n ) 'y' 'x' l? select emit ." mm" #.r ;                  

Unclear:
0 [: over in-dictionary1? section-start @ and over select ;]                        

In the case that someone wants to implement a flag-first SELECT-like
word, please do not call it SELECT.

The .S/D and .D/S case is interesting, because it shows a missed
opportunity for SELECT:

: .s/d ( -- ) 's' 'd' ss? length @ 0= s? select select emit ;       

This is still preferable flag-first IMO:

: .s/d ( -- ) s? ss? length @ 0= ffselect 's' 'd' ffselect emit ;

OTOH, if you consider both .S/D and .D/S, you can factor that further,
and then flags-last is preferable.

Anyway, it's water down the river, we won't change it.

- anton
--
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: https://forth-standard.org/
EuroForth 2023 proceedings: http://www.euroforth.org/ef23/papers/
EuroForth 2024 proceedings: http://www.euroforth.org/ef24/papers/

Date Sujet#  Auteur
31 Jan 25 * Re: Back & Forth - Co-routines12ahmed
31 Jan 25 +* Re: Back & Forth - Co-routines2ahmed
31 Jan 25 i`- Re: Back & Forth - Co-routines1ahmed
31 Jan 25 +- Re: Back & Forth - Co-routines1ahmed
31 Jan 25 +- Re: Back & Forth - Co-routines1ahmed
31 Jan 25 +* Re: Back & Forth - Co-routines6Paul Rubin
1 Feb 25 i+* Re: Back & Forth - Co-routines2ahmed
1 Feb 25 ii`- Re: Back & Forth - Co-routines1ahmed
1 Feb 25 i`* Re: Back & Forth - Co-routines3Anton Ertl
1 Feb 25 i `* Re: Back & Forth - Co-routines2ahmed
1 Feb 25 i  `- SELECT (was: Back & Forth - Co-routines)1Anton Ertl
1 Feb 25 `- Re: Back & Forth - Co-routines1mhx

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal