Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why need $ in expr?

0 views
Skip to first unread message

Chang Li

unread,
Sep 10, 2002, 7:34:29 PM9/10/02
to
Tcl's syntax rule is NO RULE!

The expr uses the C expression rather than standard Tcl way.
Are there any reasons to use $ alone with $x in the expr?
It is difficult to read and easy to make mistake.

I'd like to write a new cexpr without the $ symbol,
any suggestions?

Chang

Don Porter

unread,
Sep 10, 2002, 8:47:51 PM9/10/02
to
In article <d5224ea3.02091...@posting.google.com>, Chang Li wrote:
> The expr uses the C expression rather than standard Tcl way.
> Are there any reasons to use $ alone with $x in the expr?

A "bare word" within an argument to [expr] gets interpreted as a
function name. For example, the "sin" in [expr sin(0.0)]. Because
Tcl's [expr] command lets you define new functions (at the C level),
you don't want to interpret any bare word as a variable to be
substituted.

> I'd like to write a new cexpr without the $ symbol,
> any suggestions?

Go for it. Tcl's meant to be extended with commands that serve your
needs. If it serves other's needs as well, then by all means make it
available as a package.

I'd pick a different name, but that's just me, and not terribly
important.

--
| Don Porter Mathematical and Computational Sciences Division |
| donald...@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|______________________________________________________________________|

Roy Terry

unread,
Sep 11, 2002, 12:24:05 AM9/11/02
to
It's already been done several times. Here's
the best example I could find:
http://www.elf.org/etc/tcl-expr-patch.html

Donal K. Fellows

unread,
Sep 11, 2002, 10:58:23 AM9/11/02
to
Chang Li wrote:
> The expr uses the C expression rather than standard Tcl way.
> Are there any reasons to use $ alone with $x in the expr?
> It is difficult to read and easy to make mistake.
>
> I'd like to write a new cexpr without the $ symbol,
> any suggestions?

Supposing you changed it. How do I read from the variable called 'false' or the
variable called 'this is a silly name for a variable'? Those are both
completely valid variable names (though the second is self-defining... ;^)

Donal.
--
Donal K. Fellows http://www.cs.man.ac.uk/~fellowsd/ donal....@man.ac.uk
-- I'm curious; where does this statistic come from? Does its home, perchance,
ever see sunlight? -- Jason A Williams <jason+...@compsoc.man.ac.uk>

Chang Li

unread,
Sep 11, 2002, 11:01:59 AM9/11/02
to
Roy Terry <royt...@earthlink.net> wrote in message news:<3D7EC57A...@earthlink.net>...

Thank you very much for the reference.

The author said "All the changes have no effect on existing code."
I think it is better to create as a new command.

Chang

Larry Smith

unread,
Sep 11, 2002, 12:45:16 PM9/11/02
to

He makes a good argument, I must say. I'd like to
see a lot of it in tcl 9.0. In the meantime, a
Tcl-only solution "compute" can be found at:
http://www.wildopensource.com/larry-projects/others.html
as part of the tcl-tools package. Documentation is at:
http://www.wildopensource.com/larry-projects/tcl-tools.html#compute/

--
.-. .-. .---. .---. .-..-. | Wild Open Source Inc.
| |__ / | \| |-< | |-< > / | "Making the bazaar just a
`----'`-^-'`-'`-'`-'`-' `-' | little more commonplace."
home: www.smith-house.org | work: www.wildopensource.com

Chang Li

unread,
Sep 11, 2002, 6:58:13 PM9/11/02
to
"Donal K. Fellows" <donal.k...@man.ac.uk> wrote in message news:<3D7F5A0F...@man.ac.uk>...

> Supposing you changed it. How do I read from the variable called 'false' or the
> variable called 'this is a silly name for a variable'? Those are both
> completely valid variable names (though the second is self-defining... ;^)
>

I added Roger E Critchlow's Tcl Expr patch in the Tcl 8.3.4.
Until now it worked for all my tests. This patch is cute.
You can still use your old expr as well. To answer your
question, I tested two examples:

# test boolean
set false 0; # without this set there is a error to think false as a variable.
set x(boolean) [expr false]
% 0

# test string
set a 256
set {this is a silly name for a variable} $a
set x(name) [expr ${this is a silly name for a variable}]
% 256

Without $ in the expr there is a syntax error in expression.

But the new expr worked fine for C-style expression.
So if I limited expr in the numerial computing it is fine.

The expr is a C expresion interpreter. That is wonderful.

This example tells me that we could do further to reduce the
use of the $. For example,

* if {$x < $y} ...
if (x < y) ...

* for {set i 0} {$i < $n} {incr n} ...
for (set i 0} {i < n} {incr n}

* while {$i < $n) ...
while {i < n} ...

This could reduce many errors.

Chang

> Donal.

Don Porter

unread,
Sep 11, 2002, 7:29:36 PM9/11/02
to
Chang Li wrote:
> This example tells me that we could do further to reduce the
> use of the $. For example,
>
> * if {$x < $y} ...
> if (x < y) ...
>
> * for {set i 0} {$i < $n} {incr n} ...
> for (set i 0} {i < n} {incr n}
>
> * while {$i < $n) ...
> while {i < n} ...
>
> This could reduce many errors.

...and cause great confusion.

"Do What I Mean" languages are always really "Do What You Guess I Mean"
implementations, and turn into "Program What I Guess The Language Is
Guessing I Mean" exercises. No, thanks. Just give me a clear way to
say what I mean, and let me say it.

Bryan Oakley

unread,
Sep 11, 2002, 11:02:29 PM9/11/02
to
Chang Li wrote:
> This example tells me that we could do further to reduce the
> use of the $.

Why is that important?

I can't see how this improves the language one whit. It merely adds an
inconsistency where one doesn't presently exist. With the change,
sometimes you dereference using $, sometimes not. As it stands today,
you always need $ to dereference a variable. Makes much more sense to me.

David N. Welton

unread,
Sep 11, 2002, 11:04:32 PM9/11/02
to
Bryan Oakley <br...@bitmover.com> writes:

> I can't see how this improves the language one whit. It merely adds
> an inconsistency where one doesn't presently exist. With the change,
> sometimes you dereference using $, sometimes not. As it stands
> today, you always need $ to dereference a variable. Makes much more
> sense to me.

expr itself is an inconsistency. I wish that at least the simple
operations, + - / * and % were low level Tcl byte-code commands.
Actually, I wouldn't mind if expr dissappeared completely. I think
it's ugly.

--
David N. Welton
Consulting: http://www.dedasys.com/
Personal: http://www.dedasys.com/davidw/
Free Software: http://www.dedasys.com/freesoftware/
Apache Tcl: http://tcl.apache.org/

Michael A. Cleverly

unread,
Sep 11, 2002, 11:58:31 PM9/11/02
to
On Thu, 12 Sep 2002, David N. Welton wrote:

> Actually, I wouldn't mind if expr dissappeared completely. I think
> it's ugly.

Well, there is always:

rename expr {}

;-)

Donald Arseneau

unread,
Sep 12, 2002, 12:25:46 AM9/12/02
to

That would break existing code. For backwards compatibility:

rename expr {}
proc expr { args } {
return 42
}

renaming [expr] doesn't fix those commands (like [if]) that
use the expression calculator, but not [expr]. Someone should
report this bug.

(Just in case: ;-)


Donald Arseneau as...@triumf.ca

Chang Li

unread,
Sep 12, 2002, 4:02:40 AM9/12/02
to
Larry Smith <la...@wildopensource.com> wrote in message news:<3D7F731C...@wildopensource.com>...

> He makes a good argument, I must say. I'd like to
> see a lot of it in tcl 9.0. In the meantime, a
> Tcl-only solution "compute" can be found at:
> http://www.wildopensource.com/larry-projects/others.html
> as part of the tcl-tools package. Documentation is at:
> http://www.wildopensource.com/larry-projects/tcl-tools.html#compute/
>

I have no patience to wait for 9.0. I have used it in my codes.
The expr patch is backward compatible. You can use $ as well.
To expr the performance is more important than other commands.
By using the C-like expression we can even generate assembly
code on-the-fly for these expressions. Deal, that will be another
reason to use Tcl.

Chang

Chang Li

unread,
Sep 12, 2002, 4:19:23 AM9/12/02
to
CHA...@neatware.com (Chang Li) wrote in message news:<d5224ea3.0209...@posting.google.com>...

> This example tells me that we could do further to reduce the
> use of the $. For example,
>
> * if {$x < $y} ...
> if (x < y) ...
>
> * for {set i 0} {$i < $n} {incr n} ...
> for (set i 0} {i < n} {incr n}
>
> * while {$i < $n) ...
> while {i < n} ...
>

Above usage has been tested and worked.

A glitch is to distinguish the use of array element
and math function. So I have to use $ary(index)
to avoid the use of ary(index) in expr and the
boolean expression in if, for, and while.

I am very happy on the improvement of readiblity
of Tcl and keeping the writing errors from my C root.

Chang

>
> Chang
>
> > Donal.

et...@yahoo.com

unread,
Sep 12, 2002, 4:57:45 AM9/12/02
to
I tend to rely on the fact that when I see a $ (regardless of context)
I know I am working with data and not verbs. I believe this improves
structure and useability. I'de love to see support for $$Var and
$n$Var, viz.

set a b
set b c
set c 1
set d $$$a

as opposed to

set a b
set b c
set c 1
set d [set [set $a]]

It would certainly help with multi-dimensional and recursive coding.
Yes, I agreed would be dangerous if not properly structured. Yes, I
know there are ways to do this using procs. Yes, I realise its
frivolous, pointless even.

CHA...@neatware.com (Chang Li) wrote in message news:<d5224ea3.0209...@posting.google.com>...

Don Porter

unread,
Sep 12, 2002, 9:38:33 AM9/12/02
to
Donald Arseneau wrote:
> renaming [expr] doesn't fix those commands (like [if]) that
> use the expression calculator, but not [expr]. Someone should
> report this bug.

I think the idea is that if you're replacing [expr], you will be
replacing [if], [while], [for], as well, if that's what you're after.
That seems a better approach than having [if], etc. auto-magically
changing behavior.

Chang Li

unread,
Sep 12, 2002, 10:55:35 AM9/12/02
to
Bryan Oakley <br...@bitmover.com> wrote in message news:<9tTf9.13982$ed6....@news2.central.cox.net>...

expr is related to if, while, for commands in kernel.
so it is consistency to allow new expr expression
where the arguments are Tcl_ExprObj.

Tcl is full of inconsistency in some levels.
The $ itself is an example.
Why use $x rather than [set x]? Why use [if {$i < $n} ...]
instead of [if {[< $i $n]} ...]? To reduce the use of $
service the same purpose: make a language easy to use.

Chang

Bryan Oakley

unread,
Sep 12, 2002, 1:51:40 PM9/12/02
to
Chang Li wrote:
> Tcl is full of inconsistency in some levels.
> The $ itself is an example.
> Why use $x rather than [set x]? Why use [if {$i < $n} ...]
> instead of [if {[< $i $n]} ...]? To reduce the use of $
> service the same purpose: make a language easy to use.

I guess "easy to use" is in the eye of the beholder. I much prefer the
current situation where $var always dereferences the variable. I
wouldn't like some commands to require the $, some not.

What if I do this:

set 1 "10"
set 2 "20"
expr {1+2}

How would the computer know, in your scheme, whether I want the literal
value 1, or the variable named 1? This is a rhetorical question; I know
it is impossible for the computer to know what I intend, which is why I
like to be explicit about my variables.

While it is true that tcl has some inconsistencies, I don't see that as
a good reason to add more. But the beauty of tcl is, you can extend it
however you like. Just because I don't like a feature doesn't mean you
can't implement it for yourself.

Tom Wilkason

unread,
Sep 12, 2002, 1:58:36 PM9/12/02
to
<et...@yahoo.com> wrote in message
news:eb20d1d3.02091...@posting.google.com...

> I tend to rely on the fact that when I see a $ (regardless of context)
> I know I am working with data and not verbs. I believe this improves
> structure and useability. I'de love to see support for $$Var and
> $n$Var, viz.
>
> set a b
> set b c
> set c 1
> set d $$$a
>
> as opposed to
>
> set a b
> set b c
> set c 1
> set d [set [set $a]]
>
> It would certainly help with multi-dimensional and recursive coding.
> Yes, I agreed would be dangerous if not properly structured. Yes, I
> know there are ways to do this using procs. Yes, I realise its
> frivolous, pointless even.
>

This is consistent with the way I think of $ (kind of like the way I think
of * in C). I think this would be a useful addition.

Tom Wilkason


Joe English

unread,
Sep 12, 2002, 9:31:00 PM9/12/02
to
Chang Li wrote:
>> This example tells me that we could do further to reduce the
>> use of the $. [...]

>Above usage has been tested and worked.
>
>A glitch is to distinguish the use of array element
>and math function. So I have to use $ary(index)
>to avoid the use of ary(index) in expr and the
>boolean expression in if, for, and while.
>
>I am very happy on the improvement of readiblity
>of Tcl and keeping the writing errors from my C root.

I know the feeling; I can't count the number of times
I've accidentally written stuff like:

foreach x $xs {
(set! ys (cons (f x) ys)
}

or

if {(not . null . concatMap f) xs} {
++count
}

when programming Tcl.


--jeng...@flightlab.com

Oh, wait, yes I can. Zero.

Donal K. Fellows

unread,
Sep 13, 2002, 5:00:50 AM9/13/02
to
et...@yahoo.com wrote:
> I tend to rely on the fact that when I see a $ (regardless of context)
> I know I am working with data and not verbs. I believe this improves
> structure and useability. I'de love to see support for $$Var and
> $n$Var, viz.
> set d $$$a
> as opposed to
> set d [set [set $a]]

Experience says that it is usually easier to use either arrays or [upvar] when
doing that sort of thing, at least in the language as it currently is. BTW,
what were you interpreting $n$var to mean? The current behaviour - concatenate
the contents of two variables - is rather useful to a lot of people (myself
included)...

Chang Li

unread,
Sep 13, 2002, 8:00:37 AM9/13/02
to
Bryan Oakley <br...@bitmover.com> wrote in message news:<Mu4g9.15861$ed6....@news2.central.cox.net>...

> What if I do this:
>
> set 1 "10"
> set 2 "20"
> expr {1+2}
>

If they are constants they are considerd as constants at first.
The expr {1+2} returns 3. The expr {$1+$2} returns another result 30.

Chang

Larry Smith

unread,
Sep 13, 2002, 3:21:38 PM9/13/02
to
Joe English wrote:

> I know the feeling; I can't count the number of times
> I've accidentally written stuff like:
>
> foreach x $xs {
> (set! ys (cons (f x) ys)
> }
>
> or
>
> if {(not . null . concatMap f) xs} {
> ++count
> }
>
> when programming Tcl.

The ++ and -- notation from C made me cringe when I
first encountered it, but it is handy to provide a
visual marker for the increment/decrement point. By
comparison the "incr foo" and "incr foo -1" are not
very user friendly. This is why I designed "let" to
work the way it does. "let x ++" or "let x --" gives
you back that visual marker, and lets you forget about
the "incr" keyword.

http://www.wildopensource.com/larry-projects/tcl-tools.html#let/
http://www.wildopensource.com/larry-projects/others.html

Chang Li

unread,
Sep 14, 2002, 9:49:23 AM9/14/02
to
Larry Smith <la...@wildopensource.com> wrote in message news:<3D823AC2...@wildopensource.com>...

> Joe English wrote:
> The ++ and -- notation from C made me cringe when I
> first encountered it, but it is handy to provide a
> visual marker for the increment/decrement point. By
> comparison the "incr foo" and "incr foo -1" are not
> very user friendly. This is why I designed "let" to
> work the way it does. "let x ++" or "let x --" gives
> you back that visual marker, and lets you forget about
> the "incr" keyword.
>

I do not like the [incr x] either. I prefer the

[++ x], [-- x], [+= x $v] [-= x $v]

It is handy than current [incr x]. I do not know how many
people learn C, but even Java/C# has used the C-like
expression. For the expression the main languages are
unified. Could not understand why Tcl away from that.
Maybe in history.

Chang

> http://www.wildopensource.com/larry-projects/tcl-tools.html#let/
> http://www.wildopensource.com/larry-projects/others.html

lvi...@yahoo.com

unread,
Sep 16, 2002, 10:30:37 AM9/16/02
to

According to Chang Li <CHA...@neatware.com>:
:* if {$x < $y} ...
: if (x < y) ...

How, in the second case, do you tell whether the statement means:

compare $x to $y
or
compare "x" to $y
or
compare "x" to "y"
or
compare "$x" to "y"

? As long as tcl treats barewords as strings in some cases, variables in
some cases, and functions in some cases, there will be combinations that
are confusing.

--
Tcl'2002 Sept 16, 2002, Vancouver, BC http://www.tcl.tk/community/tcl2002/
Even if explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.
<URL: mailto:lvi...@yahoo.com > <URL: http://www.purl.org/NET/lvirden/ >

lvi...@yahoo.com

unread,
Sep 16, 2002, 10:32:40 AM9/16/02
to

According to Chang Li <CHA...@neatware.com>:
: Why use [if {$i < $n} ...]

:instead of [if {[< $i $n]} ...]?

Yikes! How ugly that second notation is!

It might work just fine for computer types, but for the non-technical
users that I have, the second notation would be terribly inconvenient.
They WANT infix algebraic expressions - and frankly, even as a computer
type, I want them as well.

Kristoffer Lawson

unread,
Sep 16, 2002, 3:23:55 PM9/16/02
to
lvi...@yahoo.com wrote:
>
> According to Chang Li <CHA...@neatware.com>:
> : Why use [if {$i < $n} ...]
> :instead of [if {[< $i $n]} ...]?
>
> Yikes! How ugly that second notation is!
>
> It might work just fine for computer types, but for the non-technical
> users that I have, the second notation would be terribly inconvenient.
> They WANT infix algebraic expressions - and frankly, even as a computer
> type, I want them as well.

I can understand that, but I also find them somehow un-Tclish. I have
a set of commands for mathematical operations that I use. The following is
clearer

set x [+ $a b]

than

set x [expr {$a + $b}]

especially in the midst of other commands.

What I don't like at all is having things like sin, cos, rand and other
mathematical functions as part of expr with a totally different syntax.
Why not just normal procedures?

--
/ http://www.fishpool.com/~setok/

et...@yahoo.com

unread,
Sep 17, 2002, 10:56:45 AM9/17/02
to
> what were you interpreting $n$var to mean?
$...to the nth..$ Bad TLA really. I also concatenate variables
together but sparingly. If the facility is there then I use it, but
code maintainability is important to me. $$$$ just seems a very clean
and simple syntax. I simply have to determine the values nth
dimensions down, which quite often is simpler than it seems.
Don. Cruickshank


"Donal K. Fellows" <donal.k...@man.ac.uk> wrote in message news:<3D81A942...@man.ac.uk>...

Glenn Jackman

unread,
Sep 17, 2002, 12:22:50 PM9/17/02
to
Kristoffer Lawson <se...@gfanrend.fishpool.fi> wrote:
> I can understand that, but I also find them somehow un-Tclish. I have
> a set of commands for mathematical operations that I use. The following is
> clearer
>
> set x [+ $a b]
>
> than
>
> set x [expr {$a + $b}]
>
> especially in the midst of other commands.
>
> What I don't like at all is having things like sin, cos, rand and other
> mathematical functions as part of expr with a totally different syntax.
> Why not just normal procedures?

Perhaps: http://wiki.tcl.tk/4085


--
Glenn Jackman
gle...@ncf.ca

0 new messages