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


Groups > comp.lang.forth > #42000

Re: ? operator in Forth

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Re: ? operator in Forth
Date 2015-09-14 18:13 +0000
Organization A noiseless patient Spider
Message-ID <mt72os$9ps$1@dont-email.me> (permalink)
References <1f86d832-a0bd-4d35-9587-0b81afe11035@googlegroups.com>

Show all headers | View raw


Mark Wills wrote:

> I quite like C's ? operator, so I knocked up one in Forth.
> 
> Since there's no way to determine the end of the false choice
> you can only put a single call in the true and false positions.
> 
> Like this:
> flag ?? wordToCallIfTrue :: wordToCallIfFalse ... ... ...
> 
> I used ?? as ? is used in Forth to display variables' contents.
> 
> Test:
> : yes ." Yes " ;
> : no  ." No " ;
> : test ( flag -- ) ?? yes :: no   ." done" ;
> 0 test No done ok:0
> 1 test Yes done ok:0 
> 
> Quite neat.
> 
> see test
> AE26 | DOCOL ZBRANCH AE32 yes BRANCH AE34 no
> AE34 | (S") done TYPE 
> AE3E | EXIT
> 
> Implementation:
> : ?? compile 0branch here 8 + , ' ,   compile branch here 4 + , ; immediate
> : :: ; immediate \ syntactic sugar
> 
> Gauntlet: Can this be done in ANS?

Oforth:

Oforth has "quotations"; these make the solution trivial.

: ?: { rot sgn abs #swap times nip perform }

>.s
Empty
ok
>-22 #[ "Success!" println] #["Failure." println] ?:
Success!
ok
>0 #[ "Success!" println] #["Failure." println] ?:
Failure.
ok
>.s
Empty
ok

-- 
The report card by the American Society of Civil Engineers showed the national
infrastructure a single grade above failure, a step from declining to the point
where everyday things simply stop working the way people expect them to. ---
washingtonpost.com/local/trafficandcommuting/us-infrastructure-gets-d-in-annual-report/2013/03/19/c48cb010-900b-11e2-9cfd-36d6c9b5d7ad_story.html

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


Thread

? operator in Forth Mark Wills <markwills1970@gmail.com> - 2015-09-10 13:55 -0700
  Re: ? operator in Forth Mark Wills <markwills1970@gmail.com> - 2015-09-10 14:16 -0700
    Re: ? operator in Forth "Elizabeth D. Rather" <erather@forth.com> - 2015-09-10 11:58 -1000
      Re: ? operator in Forth "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-11 01:30 -0400
        Re: ? operator in Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-09-11 06:17 +0000
    Re: ? operator in Forth julian.fondren@endurance.com - 2015-09-10 21:00 -0700
      Re: ? operator in Forth hughaguilar96@gmail.com - 2015-09-10 21:20 -0700
    Re: ? operator in Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-09-11 05:16 +0000
      Re: ? operator in Forth humptydumpty <ouatubi@gmail.com> - 2015-09-12 01:11 -0700
        Re: ? operator in Forth hughaguilar96@gmail.com - 2015-09-12 17:14 -0700
          Re: ? operator in Forth humptydumpty <ouatubi@gmail.com> - 2015-09-13 02:18 -0700
  Re: ? operator in Forth hughaguilar96@gmail.com - 2015-09-10 15:21 -0700
  Re: ? operator in Forth "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-09-11 01:35 -0400
  Re: ? operator in Forth humptydumpty <ouatubi@gmail.com> - 2015-09-10 22:58 -0700
  Re: ? operator in Forth Hans Bezemer <the.beez.speaks@gmail.com> - 2015-09-11 18:48 +0200
    Re: ? operator in Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-09-11 16:54 +0000
      Re: ? operator in Forth hughaguilar96@gmail.com - 2015-09-11 21:32 -0700
  Re: ? operator in Forth "WJ" <w_a_x_man@yahoo.com> - 2015-09-14 18:13 +0000
    Re: ? operator in Forth "WJ" <w_a_x_man@yahoo.com> - 2015-09-14 18:32 +0000
      Re: ? operator in Forth hughaguilar96@gmail.com - 2015-09-14 14:40 -0700

csiph-web