Author Topic: Seeking Non Recursive Procedure  (Read 2187 times)

B+

  • Guest
Seeking Non Recursive Procedure
« on: May 18, 2016, 03:15:55 AM »
I am very interested if anyone knows how to do this without recursive sub ?

Code: [Select]
'another look at doodlebot.bas SmallBASIC 0.12.6 [B+=MGA] 2016-05-17
' inspired by Rick's doodlebot!

sub recursive(size)
  local i 'x, y, angle need to be global
  for i = 1 to 3
    x2 = x + cos(angle) * size
    y2 = y - sin(angle) * size
    line x, y, x2, y2
    x = x2 : y = y2
    angle += A120
    if size > 15 then recursive size / 2 
  next
end

const A120 = 2 * pi / 3 : x = xmax / 2 : y = ymax / 2
start = min( (xmax - 20) / 2, (ymax - 20) / 2  )
? "press any to quit..."
while len(inkey) = 0
  colormode ++ : colormode = colormode % 6
  for i = 1 to 12
    recursive start
    angle  = i * pi / 6
    if colormode <= 2 then
      color (rnd * 15) \ 1 + 1
    else
      select case (i mod 4)
      case 0 : color 9
      case 1 : color 14
      case 2 : color 12
      case 3 : color 11
      end select
    fi
    showpage
    delay 200
  next
wend

From SmallBASIC forum:
Quote
Not verified on SmallBASIC, but WHILE...WEND or another loop inside a function, usually have an advantage over a recursive function:

1. A loop does the same work of a recursive function.
2. A loop is easier and faster to debug then recursive function.
3. A loop is much faster then recursive function (less overhead).
4. A loop does not require saving variables in the global scope.

While recursive function is a magic solution for lots of things - it is usually a poor solution compares to a loop within a function.

I am thinking, in this case, a recursive procedure IS a "magic solution". I do not disagree with quote in general, but for this case?

B+

  • Guest
Re: Seeking Non Recursive Procedure
« Reply #1 on: May 20, 2016, 01:55:10 AM »
OK found it, call off the search party!  ;D
« Last Edit: May 20, 2016, 09:57:47 AM by B+ »