I am currently trying to understand the implementation details of the following memoizing procedure as shown in several variants in the Tcl wiki:
proc memoize {} {
global memo
set cmd [info level -1]
if {[info level] > 2 && [lindex [info level -2] 0] eq "memoize"} return
if {![info exists memo($cmd)]} {set memo($cmd) [eval $cmd]}
return -code return $memo($cmd)
}
What is common to all of them is (to my understanding) that they compare the first word of the grandparent command (lindex [info level -2] 0
) with literally the name of this memoizing procedure (in this case, "memoize"
).
Would there be any downside of changing this to comparing the grandparent command with this command, in other words, changing [lindex [info level -2] 0] eq "memoize"
to [info level -2] eq [info level 0]
?