controlling nesting levels

From: Simon Geard (simon_at_quintic.co.uk)
Date: 11/30/04


Date: Tue, 30 Nov 2004 14:25:36 +0000

I've been looking at http://shootout.alioth.debian.org/ from the tcl
point of view. The ackerman test gets stuck because there are too many
nested calls. As far as I can see the nesting level is adjustable with
the 'interp recursionlimit' command. Trouble is that I can't seem to
improve matters no matter what I set it to.

$ tclsh84 ackerman.tcl 5
recursion limit set to 100000
Ack(3,5): 42438: 253

$ tclsh84 ackerman.tcl 6
recursion limit set to 100000
Ack(3,6) failed (74445): too many nested evaluations (infinite loop?)

Is there a limit on the permitted max value of recursionlimit that is
silently enforced or is it just that the required nesting depth for the
latter is just too big?

Simon Geard

Here is the code (slightly changed from the original) ackerman.tcl

#!/usr/bin/tclsh
# $Id: ackermann-tcl.code,v 1.4 2004/11/13 07:40:56 bfulgham Exp $
# http://www.bagley.org/~doug/shootout/

proc ack {m n} {
     incr ::depth
     if {$m == 0} {
         return [expr {$n + 1}]
     } elseif {$n == 0} {
         return [ack [expr {$m - 1}] 1]
     } else {
         return [ack [expr {$m - 1}] [ack $m [expr {$n - 1}]]]
     }
}

proc runAck {{NUM {1}}} {
     if {[string length $NUM] == 0} {
        set NUM 1
     }
     if {[catch {ack 3 $NUM} result]} {
        puts "Ack(3,$NUM) failed ($::depth): $result"
     } else {
        puts "Ack(3,$NUM): $::depth: $result"
     }
}

set ::depth 0
interp recursionlimit {} 100000
puts "recursion limit set to [interp recursionlimit {}]"

runAck [lindex $::argv 0]