by [TV], of course feel free to add/comment/correct as is normal on the wiki Starting from the ideas from [constructing mathematical formulas with Bwise blocks] we will look at the reverse of that idea here: to make a bwise canvas with blocks representing a certain repeated function call formula automatically. In general one could also take a big leap in programming, and look at what C compilers are already supposed to do for decades: [Bwise defining itself as Bwise blocks] . As an example to make clear what I mean, lets consider the formula (and its arguments, the variables s and c): set s 2 set c 8 puts [expr $s / ( (2.0 * ($c) ) + $s ) ] In this case the outcome of the expression formula is: 0.111111111111 of course we could change either or both of the variables, and get a different outcome. Clearly the expr can be written a bit shorter when taking into consideration normal precedence rules, for instance as: $s/(2.0*$c+$s), not changing the outcome when we used the [expr] rules right, and when we assume accuracy of intermedeate computations aren't influenced by our formula rewriting. For clarity as to how the outcome of this, rather random, it has no special meaning for me, formula is formed, we can prettypring the parts making up the formula, like one would in regular C language formatting conventions for instance: proc ourformula {s c} { return [expr \ \ $s \ / \ ( \ ( \ 2.0 \ * \ ( \ $c \ ) \ ) \ + \ ( \ $s \ ) \ ) \ ] } immedeately forging it into a procedure, which has the nature of what would classically be called a function: an outcome based on a functional description based on arguments. Simplified by reducing syntactically superfluous braces: proc ourformula {s c} { return [expr \ \ $s / ( \ 2.0 * $c \ + $s \ ) \ ] } Alternatively, we could us [Maxima] to render the formula in a human readable (though not typesetted) form: f(s,c):= s / ( (2 * (c) ) + s ) ; s f(s, C) := ------- 2 C + s so that we easily know what formula we're talking about, maybe in shortest form: $s/(2.0*$c+$s) for [expr]. Without embellishment(the block placementis rather random), the formula can be rendered as a graph of connected function blocks in [bwise], where c equals constant and s is slider: [http://82.168.209.239/Wiki/exaformula1.jpg] as can be seen on the abovementioned page. For Tcl without complicated Expr, the rendering on that page as nested function calls is mathematically quite sound, such as in functional analysis: divide $s [add [double $c ] $s ] where '''divide''' is a function (called procedure in tcl) which returns its first argument divided by its second, '''add''' returns the numerical sum of its two arguments and '''double''' obviously returns the numerical double of its single argument. It is quite possible, but not an excercise I'll do here, to convert ''any'' [expr]ession into such nested function call equivalent. For the moment I'll convert the last formula representation, which is easily readable for humans and computers, with some practice, to a nested list representation first, because I'll use tcl list processing to tackle its structure as the first attempt to automatically make a block structure which corresponds to it, preferably in a invertable sense. Lets define the same functions ([proc]s which have no memory or access to other data than their explicit arguments and a single return value) as on the reverse example: proc divide {num den} { return [expr $num / $den] } proc double { {in} } { return [expr 2.0*$in] } proc slider {} { return "2" } proc const {} {return 8} proc add {i1 i2} {return [expr $i1+$i2]} proc divide {num den} {return [expr $num/$den]} where I've only replaced the slider direct [Tk] access by a fixed "2". Now lets make the tcl nested funciton call into a nested list of sublists with command names and arguments, which we can then analyze and split into connected blocks: set c {divide [slider ] [add [double [const ] ] [slider ] ]} set cc [join [split [join [ split $c \[] \{] \] ] \} ] divide {slider } {add {double {const } } {slider } } I don't know split / join is the best principle here but it works easily in a short oneliner to in turn replace the [[ with { and then the ]] with } of the first result (especially special character quoting might be complicated). Now before the whole project can be attempted, I want a function which will give me the blocks at a certain distance from the final function, which I called car_deep. It returns a list of 'function' names at depth $level. It simply returns the main (last or toplevel, end of tree) function by calling it with level=0. Lets try it a nice way, and start with some extended LISP basic functions: proc car l {return [lrange $l 0 0] } proc cdr l {return [lrange $l 1 end] } proc car_repeated {list level} { set r {} foreach i $list { lappend r [car $i] } return $r } proc cdr_repeated {list level} { set r {} foreach i $list { set t [cdr $i] #puts "$i.$t." lappend r $t } return $r } proc rem_nulls { {l} } { set r {}; foreach i $l { if {$i != {}} { lappend r $i } }; return $r } proc unbrace t { set i 1000; while {[string range $t 0 0]=="\{" && [incr i -1] > 0 } { if {[llength $t] <2} { set t [lindex $t 0] } } return $t } proc car_repeated_unbrace { {list} {level} } { set r {} foreach i $list { lappend r [unbrace [car $i]] } return $r } proc cdr_unbrace_repeated { {list} {level} } { set r {} foreach i $list { set t [unbrace [cdr $i]] #puts "$i.$t." if {$t != {}} { lappend r $t } } return $r } proc cardeep { {l} {d {1}} } { set r $l # set l [unbrace $l] set l [list $l] while {[incr d -1] >= 0} { # puts "r: $r\nl: $l" set r [car_repeated_unbrace $l 0] set l [unbrace [cdr_unbrace_repeated $l 0]] } return $r } Basically the head and tail function, but then over all sublists of the offered list, and a remover of empty sublists (at first evaluation level, so {{}} would remain in place at the moment), and a remover of redundant (for my purpose) braces, and conbinations thereof. The end result is planned to be of simple repeated function call kind, with no explicit stacks or recursion, though that without question is possible, too. Now lets give de main proc 'cardeep' meaning the beginning of sublists at certain depth, our extracted list structure (from the page about constructing formulas with blocks in this case), and see if it gives us what we want: for {set i 0} {$i <6} {incr i} {puts "depth $i, cardeep: [cardeep $cc $i]"} depth 0, cardeep: divide {slider } {add {double {const } } {slider } } depth 1, cardeep: divide depth 2, cardeep: slider add depth 3, cardeep: double slider depth 4, cardeep: const depth 5, cardeep: the 0 is not really congruent, but intentional, and indeed this at first sight (and with this test data...) appears to be according to plan now. ---- [Category Mathematics] [Category Functional Programming] [Category Functional Analysis] [Category Graphics]