Hi Hugh,

In your book, you state that you know about the */ word in Forth,  
allowing you to quickly solve the resistor problem.  Why not go the  
whole hog and ship a new word in every Forth implementation:

: *+/ ( x y -- z ) 2dup + */ ;

This directly solves the problem -- any competent Forth programmer  
would be able to look at the formula for parallel resistors and  
immediately think of this solution.  It's so elegant!

But how many three-operator words would you include by default?  What  
about /-* or +// or */* or some other combination?  Which ones should  
we package into our Forth distributions?

Well, bad idea, you could never cover the number of cases that would  
potentially come up in the real world.  So how hard is it to just  
define the ones we need?

For defining /-*, you could do it by hand:

: /* ( x y z -- w ) >r / r> * ;

: /-* ( x y z -- w ) 2dup - /* ;

Great, that is quick and easy.  But how does Factor get away without  
shipping a */ word or having to define all these helper words?

The solution in Factor is to provide data flow constructs that the  
user fills in with operations, instead of combining the data flow and  
operators into the same word like */ does.

The Factor definition of *+/ looks like this:

: *+/ ( x y -- z )
     [ * ] [ + ] 2bi / ;

Similarly:

: --/ ( x y -- z )
     [ - ] [ - ] 2bi / ;

Now, this is only basic arithmetic -- what about working with  
sequences?  You might want to append or prepend two sequences, or  
merge them:

merge(append(seq1,seq2),prepend(seq1, seq2))

In Factor you can use the same approach as with the resistors:

: seqs-op ( seq1 seq2 -- seq )
     [ append ] [ prepend ] 2bi vmerge ;

"abc" "def" [ append ] [ prepend ] 2bi vmerge .
"adbecfdaebfc"

This operation, in shorthand analogous to the above, could be called  
an "APM" operation for append,prepend, merge.  You can see that the  
same combinator 2bi is useful here, without defining a ton of helper  
words.  Would you add an "AM" word to Forth to facilitate solving this  
problem?  It's just not practical.

In short, separating data flow from operators is a powerful feature.

Doug


On Oct 10, 2009, at 5:44 PM, Hugh Aguilar wrote:

> I have an updated version. Please read it through all the way,  
> rather than
> just go to the new sections, as there is a lot of rewriting  
> throughout.
> Thanks for your continued help in improving this documentation.
>
> The dvi is here: www.rosycrew.org/FactorVsForth.dvi
> The pdf is here: www.rosycrew.org/FactorVsForth.pdf
>
> Note that I have a section titled: "bad things about Factor." Don't  
> stop
> reading here though --- the next section is titled: "good things about
> Factor." The book is supposed to be balanced and fair. :-)
>
> BTW, I'm intending to present a 65c02 assembler later. I chose the  
> 65c02
> because it is quite simple, everybody is familiar with it, and there  
> are
> plenty of simulators available for it even today. The downside, of  
> course,
> is that it has very little practical value anymore. If you would  
> prefer a
> more modern microprocessor target, let me know. Of course, if you  
> have an
> opinion on this, then that also means that you're volunteering to  
> write the
> Factor version...
>
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart  
> your
> developing skills, take BlackBerry mobile applications to market and  
> stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> Factor-talk mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/factor-talk


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to