Results 1 to 3 of 3
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Yilgarn region of Toronto, Ontario
    Thanked 0 Times in 0 Posts

    Recursive- dangerous!

    I can't remember whether I spread this on the toast, but here it is. One of the best arguments EVER for being aware of the dangers of recursive functions.

    The challenge is to report back with the value of Ackerman(5,5)

    <pre>Public Function lngAckerman(ByVal intx As Integer, ByVal inty As Integer) As Long
    ' Procedure : lngAckerman
    ' Description: Calculates the Ackerman function.
    ' The Ackerman rating of a language is of interest.
    ' Calculate a small table of values for intX ranging from 0 to 5.
    ' Each column can be represented by a formula.
    ' If a language supports that formula, it has that Ackerman rating.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: Two numbers.
    ' Returns: A rather large number.
    ' Assumes: Nothing
    ' Side Effects: Will probably blow stack space on any computer
    ' in existense when Ack(5,5) is attempted.
    ' Tested: By the calls shown below.
    If intx = 0 Then
    lngAckerman = inty + 1
    If inty = 0 Then
    lngAckerman = lngAckerman(intx - 1, 1)
    lngAckerman = lngAckerman(intx - 1, lngAckerman(intx, inty - 1))
    End If
    End If
    'Public Sub TESTlngAckerman()
    ' Dim intx As Integer
    ' Dim inty As Integer
    ' For intx = 0 To 3
    ' For inty = 0 To 3
    ' Debug.Print "x=" & intx & " y=" & inty & " Ackerman(X, y)=" & lngAckerman(intx, inty)
    ' Next inty
    ' Next intx
    'End Sub
    End Function

  2. #2
    2 Star Lounger
    Join Date
    Feb 2001
    Thanked 0 Times in 0 Posts

    Re: Recursive- dangerous!

    With a subject like that, I felt I had to reply.

    Recursion is just another tool in the programmer's toolbox. The programmer needs to know and use the right tool for the right job. In the example you posted, recursion doesn't appear to be the right tool for the job.

    I don't want any programmers to shy away from recursion. Just know when it's not appropriate.

    [img]/S/sarcasm.gif[/img][img]/S/smile.gif[/img] What's next? Subject: LOOPING - DANGEROUS!


  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Sacramento, California, USA
    Thanked 1 Time in 1 Post

    Re: Recursive- dangerous!

    I agree. The same arguments can be applied to classes, inheritance, and a host of other tools that can be improperly used. And unfortunately, there is no substitute for learning how.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts