Tcl Math Syntax is Inferior to JavaScript/Python/Ruby/C/C++/Java/Perl/PHP
Problem
Tcl's mathematical expression & assignment syntax is unnecessarily verbose and thus inferior
to many other programming languages (especially Ruby, JavaScript & Python). Every extra
redundant character means:
Potential typos/errors
More information that needs to be read and processed by both people and computers
More information that needs to be typed
Tarnishing Tcl's clean, simple and beautiful space delimited syntax
Proposed Solution
Add more standard operators to tcl::mathop in Tcl 8.6, such as:
=
+= -= *= /= %= **=
&= |= ^=
<<= >>=
++ --
And add the function pi() to tcl::mathfunc.
Quick and dirty hack implementation for demonstration purposes:
proc = {var args} {
upvar 1 $var uvar
set uvar [expr $args]
}
proc += {var args} {
upvar 1 $var uvar
set uvar [expr $uvar + ($args)]
}
proc ++ var {
upvar 1 $var uvar
incr uvar
}
Examples/Comparisons of Clarity & Line Lengths
Assign Result of Single Operator Expression
s = 60 * 60 * 24 # JavaScript, Python, Ruby
s = 60 * 60 * 24; # Perl, PHP
int s = 60 * 60 * 24; # C, C++, Java
set s [expr 60 * 60 * 24] # Tcl
set s [* 60 60 24] # Tcl 8.5
= s 60 * 60 * 24 # Proposed Tcl 8.6
Assign Result of Multiple Operator Expression (Long Version)
y = m * x + b # JavaScript, Python, Ruby
$y = $m * $x + $b; # Perl, PHP
float y = m * x + b; # C, C++, Java
set y [expr $m * $x + $b] # Tcl
= y $m * $x + $b # Proposed Tcl 8.6
= y m * x + b # Future Fantasy Tcl: lookup
# unrecognized tokens as variables/procedures
Assign Result of Multiple Operator Expression (Short Version)
y=m*x+b # JavaScript, Python, Ruby
$y=$m*$x+$b; # Perl, PHP
float y=m*x+b; # C, C++, Java
set y [expr $m*$x+$b] # Tcl
= y $m*$x+$b # Proposed Tcl 8.6
= y m*x+b # Future Fantasy Tcl: lookup
# unrecognized tokens as variables/procedures
Assign Result of Mixed Expression
$x = sin( pi() / 2 ); # PHP
x = Math.sin Math::PI / 2 # Ruby
x = Math.sin( Math.PI / 2 ) # JavaScript
x = math.sin( math.pi / 2 ) # Python
double x = Math.sin( Math.PI / 2 ); # Java
const double PI = acos( -1 ); # C, C++: do it yourself Pi
double x = sin( PI / 2 );
use constant PI => atan2( 1, 1 ) * 4; # Perl: do it yourself Pi
$x = sin( PI / 2 );
proc pi {} { expr acos( -1 ) } # Tcl: do it yourself Pi
set x [expr sin( [pi] / 2 )]
= x sin( pi() / 2 ) # Proposed Tcl 8.6
= x sin pi / 2 # Future Fantasy Tcl: optional parenthesis
Accumulate
s += 60 # JavaScript, Python, Ruby
s += 60; # C, C++, Java, Perl, PHP
incr s 60 # Tcl
+= s 60 # Proposed Tcl 8.6
Increment
++x # JavaScript
++x; # C, C++, Java, Perl, PHP
x += 1 # Python, Ruby
incr x 1 # Tcl
incr x # Tcl 8.5
++ x # Proposed Tcl 8.6
Post Script
If this has been previously recommended/discussed then could someone provide a link?
I know this wiki may not be the ideal place to propose this, but it is the quickest.