Hofstadter男性 - 女性序列在Forth

时间:2014-05-07 05:22:13

标签: sequence forth

我正在尝试在Forth中编写Hofstadter sequence,而不使用递归。我觉得我非常接近,但我一直在收到一个告诉我的错误,

  

TypeError:x [m [w]]不是函数

当我使用10 MALE运行它时。我该如何解决这个错误?

我的代码如下:

VARIABLE SIZE
VARIABLE MALE-ARR
VARIABLE FEMALE-ARR

: MALE
    DUP SIZE !
    CREATE MALE-ARR SIZE @ CELLS ALLOT \ CREATE MALE ARRAY OF LENGTH N
    CREATE FEMALE-ARR SIZE @ CELLS ALLOT \ CREATE FEMALE ARRAY OF LENGTH N
    0 MALE-ARR ! \ MALE-ARR(0) = 0
    1 FEMALE-ARR ! \ FEMALE-ARR(0) = 1
    SIZE @ 1 DO
        i FEMALE-ARR MALE-ARR i 1- CELLS + @ CELLS + @ - MALE-ARR i CELLS + !
        i MALE-ARR FEMALE-ARR i 1- CELLS + @ CELLS + @ - FEMALE-ARR i CELLS + !
    LOOP
    SIZE @ 0 DO
        MALE-ARR i CELLS + @ .
    LOOP
    ; 

1 个答案:

答案 0 :(得分:1)

也许是这样的?

: array ( u "name" -- )   create cells allot  does> swap cells + ;

10 constant size
size array male
size array female

: compute ( -- )   0 0 male !  1 0 female !                                     
   size 1 do                                                                    
      i i 1- female @ male @ - i female !                                       
      i i 1- male @ female @ - i male !                                         
   loop ;

: print ( u -- )   0 do i male @ . loop ;