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.

This is a program in Forth:

: Wondrous ( n -- )
  Begin NotEnd? While
  NumberType? RuleApply Output
  Repeat Drop Cr ;

: NotEnd? ( n -- n,f ) Dup 1 > ;
: NumberType? ( n -- n, f ) Dup 2 Mod 0 = ;
: RuleApply ( n -- n ) If EvenRule Else OddRule Then ;
: EvenRule ( n -- n ) 2/ ;
: OddRule ( n -- n ) 3 * 1+ ;
: OutPut ( n -- n ) Dup . ;

There's no tally here which I could add.

So far I have not been able to reproduce this in Factor. I can not work out the 
correct syntax for the While loop.

Can anyone give an example or 2 using simple loops in Factor.




                                          
------------------------------------------------------------------------------
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