Re: [colorforth] if then else
- Subject: Re: [colorforth] if then else
- From: Nick Maroudas <alice@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 19 Oct 2008 15:04:42 +0200
Quoting Jason Kemp <jason.kemp@xxxxxxxxxxxxxxxx>:
>... " Good evening,
>... "
>... " I can’t find an ‘else’ so how does one construct
>... " an if-then-else? Should
>... " it really be done in two words:
>... "
>... " : word1 if ... ; then ... ;
>... " : word2 stuff-before word1 stuff-after ;
>... "
>... " Thanks,
>... " Jason
>... "
>... "
Nick here: I always get confused by logic so I made up
a simple example. Word1 does nothing but observe
whether n was 1 or else 2, then (whatever the number)
word2 adds 10: is this if-else-then?
( n) : word1 dup ( nn) -1
"If" jumps to the next "then" while " ; " returns to
the
calling word (word2). One can concatenate " ; " for
instance word1 has 3 alternatives - the final one drops
any numbers other than 1 or 2, and replaces them by 3,
so the only results from word2 are 11, 12 and 13.
I think one can also omit intermediate " ; " (but
not the final return ;) to make this crude one-word
form of if-else-then
( n) : word1 dup ( nn) -1 + drop ( n) 0if ( n,f)
drop
1 ( no ";" ) then ( n/1) dup ( nn/11) -2 + drop 2 then
(
still no ";") 3 + ;
Results from this word1 are 0>>3 1>>4 2>>5 n>>n+3.
The recognized n 1 and 2 can be manipulated - but,
without their own individual ";" to call each one back
home they will be further processed while passing
along the chain of ifs to the final common return ";".
Caritas,
Nick
---------------------------------------------------------------------
To unsubscribe, e-mail: colorforth-unsubscribe@xxxxxxxxxxxxxxxxxx
For additional commands, e-mail: colorforth-help@xxxxxxxxxxxxxxxxxx
Main web page - http://www.colorforth.com