Version 3 of Playing LISP

Updated 2001-12-19 10:39:26

Richard Suchenwirth - Here's a first approximation of making Tcl simulate LISP - there are subtle differences yet to solve, e.g. the $x formulation below - but build on it, and enjoy!

The wrapper proc is called, like in LISP, progn ("evaluate the following sequentially, and return the n-th(=last) result"). It takes care of paren-to-brace mapping, and then does that:

 proc progn body {
     regsub -all {;[^\n]*\n} $body \n body
     set body [string map {( \{ ) "\} "} $body]
     foreach cmd $body {
         set _ [uplevel 1 $cmd]
     }
     set _
 }

# Setting up Polish-style arithmetic prefix operators...

 foreach op {+ - * /} {
     proc $op args [string map [list @op@ $op] {expr [join $args @op@]}]
 }

# Some vocabulary exercises...

 interp alias {} defun {} proc
 interp alias {} setq  {} set

# Now testing...

 set res [progn {
     ;; LISPish comments, removed before bracing
     (defun sq (x) (* $x $x))
     (setq y 3)
     (sq $y)
 }]
 puts $res,y:$y

Tcl and LISP - Arts and crafts of Tcl-Tk programming