Re: [gforth] If then else in interpreted mode

2013-05-02 Thread John Allsup

Thanks,

I'm quite new to Forth itself, so haven't learned the various 
simplifying shortcut words.  Also, I may tend not to use them when 
illustrating an idea to ask others about it.  This was motivated by my 
reading the gforth manual 3.16 'In Forth you can use control structures 
only inside colon definitions.' and thinking about ways round this.


Of the two simplifying suggestions, I note that abs would at some point
in its implementation need to do a branch since it does not know that 
the result of 0 is either -1 0 or 1.


The thought was that where you have a construct such as:
BEGIN
...a
cond
WHILE
...b
REPEAT
you can factor the ...a, ...b and cond bits into words and then
define a control structure word that you feed execution tokens.

This is much like what happens in C where you have either
if( a == b ) statement;
or
if( a == b ) { block; of; statements; }
and here you achieve this by giving a block a name.  Thus
if( a == b ) WORD;
and factoring as WORD := { block; of; statements; }.

The jif example and the : fy pick execute ; seemed to give an easy 
example, so I thought I'd throw it onto the list as a way of saying hi 
and getting feedback.  ( The nice thing about fy defined this way is 
that you can define various conditional execution words and name them 
after the roots of your favourite adjectives, for example fluf puf buf 
duf -- it make seem and in fact be unprofessional, but cute and cuddly 
names are far easier to remember intuitively if you have a mind like 
mine, for the reason that concepts learned as a young child are more 
efficiently represented in my mind than abstract stuff learned at uni. )


As an aside, if we have already defined
: a 1 1 + ;
and
: b 2 a * 3 a * + ;
is there a way to get at the definition of b (like with see) so that
one could search for a's in the definition, expand some or all of them
with their current definition and turn the result into a new word, say 
c.  In this example, a function that, given a and b produces the effect of

: c 2 1 1 + * 3 1 1 + * + ;
and indeed also if we had defined
: d 4 3 + ;
being able to produce (i.e. search and replace)
: e 2 d * 3 d * + ;
and
: e 2 4 3 + * 3 4 3 + * + ;

(I'm attempting to present trivial examples first so as not to have real 
world examples's real world purposes distracting from what is 
essentially a pure theoretical question.)


John


On 01/05/2013 23:57, Marcos Cruz wrote:

En/Je/On 2013-05-01 23:15, John Allsup escribió / skribis / wrote :

Hi John, welcome!

As you show, execution tokens (on the stack, in lists or any other data
structure), can be used to make decisions based on calculations. That's
a powerful feature of Forth.


 : jif 0 = if 0 else 1 then ;


A simpler alternative:

: jyf 0 abs ;

Usually in Forth a calculation can be used instead of a condition.


so as to have two nicely named words: jiffy and jemmy that do the
work of interpret time conditional execution.


You can use also '[if]', '[else]' and '[then]'. They work quite
differently than 'if', 'else' and 'then', but they provide the same
syntax.


As to taking this much further, we hit a place where it is really
nice to have more stacks (since you ideally want an operator stack
to do the picking from rather than the data stack


There are several Forth extra stack tools you can use. I think they are
not hard to find on the web. Anyway the simpler implementations need
only a few lines of code.

Cheers,

Marcos




--
: jda . John D. Allsup . woz 'ere . yeah! ; immediate jda
: website . allsup.co and . chalisque.com ; 1 1 website if
: blog . deardiary.chalisque.org ; immediate then
( no, I haven't checked whether this actually runs... )



Re: [gforth] If then else in interpreted mode

2013-05-01 Thread Josh Grams
On 2013-05-02 12:57AM, Marcos Cruz wrote:
On 2013-05-01 23:15, John Allsup wrote :

 : jif 0 = if 0 else 1 then ;

A simpler alternative:

   : jyf 0 abs ;

Yikes!  How about:

: jif 0 1 and ;

Usually in Forth a calculation can be used instead of a condition.

Well, that's true in a lot of languages, it's just that most other
language communities condemn it as bad style, and it's more acceptable in
the Forth culture.

I will sometimes AND a value with a flag, giving the value or zero.
Beyond that I usually find them hard to read, so I avoid them.

But then I'm not doing tiny embedded stuff, so I don't have any
situations where I consider those kinds of tiny CPU-time savings to be
worth the extra programmer time writing and maintaining the code.

YMMV, etc.

--Josh