Home > Archive > Tcl > June 2006 > I don't like expr
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| casioculture@gmail.com 2006-06-24, 8:23 am |
|
http://wiki.tcl.tk/1656
I'm learning forth and lisp these days, and one thing they tought me is
how much I like the polish notation. It's quite elegant. Tcl uses a
lisplike polish notation for commands but the expr remains algebraec,
which I find ugly. Why doesn't tcl adopt a complete polish notation? By
now it's clear that it is an elite niche language and it doesn't need
to compromise to make itself appealing to the popular-and-fashionable
crowd who won't convert to it.
| |
| Michael Schlenker 2006-06-24, 8:23 am |
| casioculture@gmail.com wrote:
>
> http://wiki.tcl.tk/1656
>
> I'm learning forth and lisp these days, and one thing they tought me is
> how much I like the polish notation. It's quite elegant. Tcl uses a
> lisplike polish notation for commands but the expr remains algebraec,
> which I find ugly. Why doesn't tcl adopt a complete polish notation? By
> now it's clear that it is an elite niche language and it doesn't need
> to compromise to make itself appealing to the popular-and-fashionable
> crowd who won't convert to it.
>
You can write your own polish version of expr, and use it without
problems in a private namespace. You could radically change the ::expr
but expect all kinds of breakage if you do.
But i don't think Tcl will adopt a expr with polish notation anytime
soon if ever. The factor isn't the new pop culture crowd your refering
to, but the huge amounts of code and tests already existing, which would
need to be upgraded, fixed, reviewed.
But, if your really devoted to 'fix' expr to fit your mindset and want
to make the change, file a TIP for Tcl 9.0, to change expr or a TIP for
8.5 with a new pexpr command, which works the way you like, best with a
working sample implementation and tests.
Maybe the TCT likes the proposal, but i doubt it.
Michael
| |
| Arjen Markus 2006-06-24, 8:23 am |
|
Michael Schlenker schreef:
> casioculture@gmail.com wrote:
> You can write your own polish version of expr, and use it without
> problems in a private namespace. You could radically change the ::expr
> but expect all kinds of breakage if you do.
>
> But i don't think Tcl will adopt a expr with polish notation anytime
> soon if ever. The factor isn't the new pop culture crowd your refering
> to, but the huge amounts of code and tests already existing, which would
> need to be upgraded, fixed, reviewed.> Maybe the TCT likes the proposal, but i doubt it.
One reason the infix notation is popular is that it has been around for
a few centuries.
Everybody (at least in Western Europe and the Americas, as far as I
know) learns it.
Maths books are full of it.
I can up to a point understand your point of view, but personally, I
rather like [expr] and
rather wished it were easier to enhance it - to get it to work with
lists and perform
element-wise operations, to use it with complex numbers and more ...
Myself and
others have done quite a bit of work in that direction.
But, as Michael points out, you can overcome the infixedness of [expr]
by writing
your own Polish version :)
Regards,
Arjen
| |
| Gerald W. Lester 2006-06-24, 8:23 am |
| Arjen Markus wrote:
> Michael Schlenker schreef:
>
....[color=darkred]
> But, as Michael points out, you can overcome the infixedness of [expr]
> by writing your own Polish version :)
And while you are at it, for completeness you may want to also write rpexpr
that implements a Reverse Polish version.
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
| |
| Jonathan Bromley 2006-06-24, 8:23 am |
| On Thu, 22 Jun 2006 08:39:37 -0500, "Gerald W. Lester"
<Gerald.Lester@cox.net> wrote:
>And while you are at it, for completeness you may want to also write rpexpr
>that implements a Reverse Polish version.
Hsilop is quite tricky to implement in Tcl, I think.
is Forward-Polish easy:
proc + {L R} {
return [expr {$L+$R}]
}
proc - {L R} .... etc, etc
+ [+ 3 5] [+ 7 9]
--
Jonathan Bromley, Consultant
DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services
Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan.bromley@MYCOMPANY.com
http://www.MYCOMPANY.com
The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
| |
| suchenwi 2006-06-24, 8:23 am |
|
Jonathan Bromley schrieb:
>
> Hsilop is quite tricky to implement in Tcl, I think.
Is it? Here's a little RPN engine, including a tiny test suite :)
#-- Polish operators (+ a b)
foreach op {+ - * / %} {proc $op {a b} "expr {\$a $op \$b}"}
#-- RPN engine
proc rpn args {
foreach a $args {
if {[info commands $a] eq $a} {
swap; push [$a [pop] [pop]]
} else {
push $a
}
}
set ::S
}
#-- Stack routines
interp alias {} push {} lappend ::S
proc pop {} {
if ![llength $::S] {error "stack underflow"}
K [lindex $::S end] [set ::S [lrange $::S 0 end-1]]
}
proc swap args {push [pop] [pop]}
proc K {a b} {set a}
#-- Test suite
proc e.g. {cmd -> expected} {
catch {uplevel 1 $cmd} res
if {$res ne $expected} {puts "$cmd -> $res, expected $expected"}
}
e.g. {rpn 3 4 +} -> 7
e.g. {rpn 42 7 /} -> {7 6} ;# because the last 7 is still on stack
| |
| suchenwi 2006-06-26, 4:21 am |
|
stephanearnold@yahoo.fr schrieb:
> There is a bug in your rpn proc : try [rpn 2 3 *] and you'll see it
> fails.
> Why ? Because [info commands *] isn't the string "*"
Right - that comes from not testing every possibility of the code... :(
It works better (and cleans the stack more properly) with
proc rpn args {
foreach a $args {
if {[in [info commands $a] $a]} {
swap; push [$a [pop] [pop]]
} else {
push $a
}
}
pop
}
where (another of those ever-useful one-liners, at least before Tcl
8.5):
proc in {list el} {expr {[lsearch -exact $list $el]>=0}}
|
|
|
|
|