The idiom for <=> is that the result is eventually consumed by a case.
For example:
: do-something ( n m -- )
<=> {
{ +lt+ [ "Less" ] }
{ +gt+ [ "Greater" ] }
{ +eq+ [ "Equal" ] }
} case print ;
With locals, you can implement this ?sgn combinator in terms of case as this:
:: ?sgn ( cmp lt gt eq -- )
cmp {
{ +lt+ [ lt call ] }
{ +gt+ [ gt call ] }
{ +eq+ [ eq call ] }
} case ; inline
(You could just as well implement this in terms of if, and without
locals, as you did below, but this is a little cleaner-looking. The
implementation could look like this:
: ?sgn ( value negative-combinator zero-combinator positive-combinator -- )
roll ! -- neg zer pos value
dup +gt+ = [ drop 2nip call ] [
nip ! -- neg zer value
+eq+ = [ nip ] [ drop ] if call
] if ;
In both combinators, the value/cmp input is +gt+, +eq+ or +lt+)
<=> used to return 1, 0 or -1, but it was changed to this style to
make it more clear what the output means, and to make things more
strongly typed. If <=> returns a number, then it might accidentally be
used in an arithmetic calculation, where it wouldn't have a real
meaning. Using special symbols prevents that (with a runtime type
error).
If you want to make your code available, you can set up a git
repository (eg at github) and upload the code there. It's easiest for
others to get your code from you if you put a full copy of the Factor
repository, with your modifications, there, rather than just the code
that you've written.
Dan
On Thu, Apr 30, 2009 at 2:42 PM, Hugh Aguilar <[email protected]> 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