Hugh,

I hope that an example will make things more clear:

: ?sgn ( value neg zero pos -- )
     roll 0 <=> {
         { +lt+ [ 2drop call ] }
         { +eq+ [ drop nip call ] }
         { +gt+ [ 2nip call ] }
     } case ; inline

: doit ( n -- ) [ "lt" print ] [ "eq" print ] [ "gt" print ] ?sgn ;

5 doit

"abcd" "zyxw" [ first ] compare doit


Factor used to return -1, 0, and 1 but it's higher-level to return  
symbols than magic numbers.  There's an 'invert-comparison' word to  
invert the +lt+ and +gt+ symbols.

Your code is calling quotations with arbitrary stack effects so it  
should be inlined.  Words that call this combinator can be compiled,  
but the combinator itself doesn't compile and no error is thrown by  
the compiler because this is a Factor idiom used everywhere.

There's a word in the system vocabulary called 'exit' that quits  
Factor and returns an integer to the shell.  I'm not entirely sure if  
this is Forth's EXIT or not.

If you want to contribute code (we love contributed code!), set up a  
git repository on github or elsewhere and send a message to the  
mailing list or talk to Slava or me (erg) in #concatenative on  
irc.freenode.org.

Have fun,
Doug

On Apr 30, 2009, at 2:42 PM, Hugh Aguilar wrote:

> Here is a function that I wrote:
>
> : ?sgn ( value negative-combinator zero-combinator positive- 
> combinator -- )
>    roll                                        ! -- neg zer pos value
>    dup 0 > [ drop  2nip  call ] [
>        nip                                     ! -- neg zer value
>        0 = [ nip ] [ drop ] if  call
>    ] if ;
>
> My question is, why does the <=> function return the values: +lt+,  
> +eq+ and
> +gt+? Wouldn't it make more sense for it to return 1-, 0 and 1? This  
> is what
> the COMPARE function does in ANS-Forth. This results in a kind of
> polymorphism because a function such as my ?sgn will work whether it  
> follows
> an integer subtraction or a call to COMPARE, or what not. In Factor  
> I would
> have to write another version of ?sgn specific to <=>. This is like  
> how C
> function names often have the type prefixed onto them, and there are
> multiple versions of the same function for the various data types.  
> That is
> one of the many reasons why C is difficult to program in. Note that  
> I'm not
> criticizing Factor; I'm just trying to learn Factor's philosophy.  
> I've long
> noted many problems with ANS-Forth and have been looking for something
> better --- which might be Factor.
>
> One other quick question: Does Factor have something like the EXIT  
> function
> of ANS-Forth?
>
> Also, I am translating a symbol-table program into Factor (originally
> written in IBM370 assembly-language and later upgraded to ANS- 
> Forth). How do
> I go about making this available to the Factor community? This uses an
> algorithm that I invented myself, which provides faster execution  
> speed than
> hash-tables IMHO.
>
>
> ------------------------------------------------------------------------------
> Register Now & Save for Velocity, the Web Performance & Operations
> Conference from O'Reilly Media. Velocity features a full day of
> expert-led, hands-on workshops and two days of sessions from industry
> leaders in dedicated Performance & Operations tracks. Use code  
> vel09scf
> and Save an extra 15% before 5/3. http://p.sf.net/sfu/velocityconf
> _______________________________________________
> Factor-talk mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/factor-talk


------------------------------------------------------------------------------
Register Now & Save for Velocity, the Web Performance & Operations 
Conference from O'Reilly Media. Velocity features a full day of 
expert-led, hands-on workshops and two days of sessions from industry 
leaders in dedicated Performance & Operations tracks. Use code vel09scf 
and Save an extra 15% before 5/3. http://p.sf.net/sfu/velocityconf
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to