Neil Madden

Email
The title of this page with '.E.' between the words AT a well known email provider beginning with g...
Web
http://neilmadden.wordpress.com/about/
    Location:  Stroud, Gloucestershire, UK.

Currently employed as “Security Director” at [L1 ], making sure all their products are secure and don't do anything silly with cryptography. Previously, I gained my PhD in Computer Science from the University of Nottingham in the UK.

Memory lane: http://web.archive.org/web/20010630053409/http://mini.net/tcl/976.html (I'd almost forgotten about mini.net!)

Archive.org also has copies of my old university Tcl page: http://web.archive.org/web/20071020031707/http://www.cs.nott.ac.uk/~nem/tcl/ and my main page from just before I left: http://web.archive.org/web/20071020031707/http://www.cs.nott.ac.uk/~nem/

Pages I have created (see NEM for a more comprehensive list of contributions to this wiki):


Experimenting with a Tcl version of 3-Lisp's reflective lambdas. In 3-Lisp, a reflective function receives 2 extra arguments: the environment of the caller and the continuation of the caller. IIRC, it also receives the other arguments in unevaluated form. This allows you to do loads of things: lazy evaluation, various kinds of meta programming etc. They are pretty much as powerful as Lisp macros, but I think they fit Tcl's model much better - e.g. they could be a replacement for uplevel that doesn't explicitly reference the stack. Anyway, here's a first stab at emulating them in current Tcl using uplevel/upvar:

 namespace eval meta {
     namespace export proc apply
     namespace ensemble create
     ::proc proc {name params body} {
         interp alias {} $name {} ::meta::apply [list $params $body ::]
     }
     ::proc apply {proc args} {
         set env [capturelevel 1]
         uplevel 1 [linsert $args 0 ::apply $proc $env]
     }
     ::proc capturelevel level {
         incr level
         set env [dict create]
         foreach name [uplevel $level { info vars }] {
             upvar $level $name value
             catch { dict set env $name $value } ;# no arrays
         }
         return $env
     }
 }

This version is very limited: it captures the caller's variable environment as a dict and passes it to the meta-proc. So, read-only and variables-only. Still, it demonstrates the idea. Now, imagine this was read/write, and that the environment contained vars and commands. (Perhaps if we unified command/var names in Tcl 9?) Now also imagine that you get the current continuation as another arg...

Example:

 % meta proc get {env name} { dict get $env $name }
 get
 % proc foo {a b} { puts "a = [get a], b = [get b]" }
 % foo 1 2
 a = 1, b = 2

21 Jan 2007: A little filter I've been using for summarising undergraduate coursework marks. This is too trivial to deserve a page of its own, but I've found it useful on several occassions. Expects stdin to contain a single integer on each line, between 0-100, and generates a little statistical summary. Uses the tcllib math::statistics package, which makes things easy.

 package require math::statistics 0.2
 namespace import math::statistics::basic-stats math::statistics::histogram
 proc % {value total} { expr {round($value/double($total)*100)} }

 set marks [split [string trim [read stdin]] \n]
 lassign [basic-stats $marks] mean min max num std var
 puts "Count:        $num"
 puts "Max:          $max"
 puts "Min:          $min"
 puts "Mean:         $mean"
 puts "Variance:     $var"
 puts "Std. Dev.:    $std"
 set bands [list 10 20 30 40 50 60 70 80 90]
 foreach band [linsert $bands 0 0] value [histogram $bands $marks] {
     puts [format {%2d-%2d: %-40s %3d (%2d%%)} $band [expr {$band+9}] \
          [string repeat "#" $value] $value [% $value $num]]
 }

Example output (from some random numbers):

 Count:        100
 Max:          99.0
 Min:          1.0
 Mean:         50.19
 Variance:     741.3675757575759
 Std. Dev.:    27.22806595697858
  0- 9: #######                                    7 ( 7%)
 10-19: #########                                  9 ( 9%)
 20-29: #################                         17 (17%)
 30-39: #####                                      5 ( 5%)
 40-49: #############                             13 (13%)
 50-59: ##############                            14 (14%)
 60-69: ###########                               11 (11%)
 70-79: ######                                     6 ( 6%)
 80-89: ########                                   8 ( 8%) 
 90-99: ##########                                10 (10%)