On Sep 15, 2011, at 8:58 PM, graham telfer wrote:

> Hi,
> 
> I am trying to get to grips with Factor. My first attempt is to write a 
> program to the following spec:
> 
> Pick some positive integer and call it n.
> If n is odd, multiply it by three and add one.
> If n is even, divide it by two.
> Continue this process until n is equal to one.

In Factor, while and if are higher-order functions that take their branches as 
quotations, bracketed by [ ]. So your example would be:

: wondrous ( n -- )
    [ dup 1 = not ] [ dup odd? [ 3 * 1 + ] [ 2 / ] if dup . ] while drop ;

Factor has extensive reference documentation. To find help on a topic, try the 
apropos word to search the docs:

"while" apropos

Or if you know the name of a word, use the help word to look up its 
documentation:

\ while help

-Joe

Attachment: smime.p7s
Description: S/MIME cryptographic signature

------------------------------------------------------------------------------
BlackBerry® DevCon Americas, Oct. 18-20, San Francisco, CA
http://p.sf.net/sfu/rim-devcon-copy2
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to