再帰

再帰処理のテキストを書いていたら、なんでもいいから再帰アルゴリズムを用いたプログラムを組んでみたくなった。その何種類かを以下に挙げる。

ハノイの塔

sub Solve(n,a$,b$,c$)
   if n>0 then
      call Solve(n-1,a$,c$,b$)
      print a$;"→";b$
      call Solve(n-1,b$,a$,c$)
   end if
end sub

アッカーマン関数

function ack(m,n)
   if m=0 then
      let ack = n+1
   elseif n=0 then
      let ack = ack(m-1,1)
   else
      let ack = ack(m-1,ack(m,n-1))
   end if
end function