Petr Schreiber
12-05-2010, 18:44
Learning for exam,
I had to create example on different kinds of recursion to really see they behave as described.
Could serve as some kind of test whether recursion works ok:
' -- Various types of recursion
Uses "Console"
PrintL "Ackermann function"
PrintL ack(3, 4)
PrintL
PrintL "Fibonacci"
PrintL fib(5)
PrintL
PrintL "Factorial"
PrintL fact(5)
PrintL
PrintL "Greatest common divisor"
PrintL gcd(513, 27)
PrintL
WaitKey
' -- Nested recursion: Recursive call to function contains recursive call to itself in the arguments
' -- Ackermann function
Function ack(x As Number, y As Number) As Number
If x = 0 Then Return y+1
If y = 0 Then Return ack(x-1, 1)
Return ack(x-1, ack(x, y-1))
End Function
' -- Cascade (tree) recursion: Multiple occuriences of recursive call appear in one expression, but without any nesting
' -- Fibonacci
Function fib(n As Number) As Number
If n <= 1 Then Return 1
Return fib(n-2)+fib(n-1)
End Function
' -- Linear recursion: Each alternative of definition contains 1 recursive call at max.
' -- Factorial
Function fact(n As Number) As Number
If n = 0 Then Return 1
Return n * fact(n-1)
End Function
' -- End recursion: Special case of linear recursion, where recursive call is the last operation of specific alternative of definition
' -- Greatest common divisor
Function gcd(x As Number, y As Number) As Number
If x = y Then Return x
If x > y Then Return gcd(x-y, y)
Return gcd(x, y-x)
End Function
You can take a walk through the code using debugger, to watch it call itself.
Petr
I had to create example on different kinds of recursion to really see they behave as described.
Could serve as some kind of test whether recursion works ok:
' -- Various types of recursion
Uses "Console"
PrintL "Ackermann function"
PrintL ack(3, 4)
PrintL
PrintL "Fibonacci"
PrintL fib(5)
PrintL
PrintL "Factorial"
PrintL fact(5)
PrintL
PrintL "Greatest common divisor"
PrintL gcd(513, 27)
PrintL
WaitKey
' -- Nested recursion: Recursive call to function contains recursive call to itself in the arguments
' -- Ackermann function
Function ack(x As Number, y As Number) As Number
If x = 0 Then Return y+1
If y = 0 Then Return ack(x-1, 1)
Return ack(x-1, ack(x, y-1))
End Function
' -- Cascade (tree) recursion: Multiple occuriences of recursive call appear in one expression, but without any nesting
' -- Fibonacci
Function fib(n As Number) As Number
If n <= 1 Then Return 1
Return fib(n-2)+fib(n-1)
End Function
' -- Linear recursion: Each alternative of definition contains 1 recursive call at max.
' -- Factorial
Function fact(n As Number) As Number
If n = 0 Then Return 1
Return n * fact(n-1)
End Function
' -- End recursion: Special case of linear recursion, where recursive call is the last operation of specific alternative of definition
' -- Greatest common divisor
Function gcd(x As Number, y As Number) As Number
If x = y Then Return x
If x > y Then Return gcd(x-y, y)
Return gcd(x, y-x)
End Function
You can take a walk through the code using debugger, to watch it call itself.
Petr