From: Simon Geard on
On 04/01/2010 17:55, Richard Owlett wrote:
> Geard wrote:
>> On 03/01/2010 06:42, Bill Waddington wrote:
>>> With the kind help of some folks here I've gotten my first
>>> tcl/tk code up and running. It's just a toy program to try
>>> to get a feel for this language - a little sudoku solver.
>>>
>>> It seems to work OK. Doesn't solve all the hard puzzles,
>>> but that wasn't really its purpose, and I haven't given
>>> much thought to the algorithms. It could use things like
>>> roll-back, and a nice puzzle generator. Those are later
>>> exercises, if at all.
>>>
>>> To the point: I have some experience with C - writing device
>>> drivers - but this is my first tast of tcl/tk. One thing I'm
>>> sure of is that this isnt' even close to idiomatic tcl/tk.
>>>
>>> It's probably distorted by trying to be C code. Abuse of
>>> "global", not putting widget names in variables, weird division
>>> of proc code and main (or whatever it's called) code, etc, etc.
>>>
>>> Anyone up to doing a little code review? I would particulary
>>> appreciate how to use more tcl-isms, and how one would/should
>>> divide up the code into procs.
>>>
>>> http://www.beezmo.com/scratch/sudoku/newsudoku.tcl
>>>
>>> Sorry about the long post. I should have just said "easy on
>>> the flames, I'm clueless".
>>>
>>> Thanks,
>>> Bill
>>
>> Bill,
>>
>> I think this is very good particularly for a first attempt! My
>> suggestions for improvements are the following:
>>
>> 1) Always use {} in expr even if the calculation is simple since it
>> avoids double substitution, e.g.
>> [expr {$index + 1}]
>>
>> 2) Always use {} in if constructs for the same reason, e.g.
>> if {[allCheck $cellarray($index)]} {
>>
>> 3) to compare strings you can use eq or ne, e.g.
>> if {$x eq { } || $x eq {}} {
>>
>> 4) I'm not sure if it's an exact equivalent, but if it is you could use
>> the builtin tk_messageBox instead of popMessage
>>
>> 5) In terms of organization, I would use a namespace to provide more
>> containment, e.g.
>>
>> namespace eval Board {
>> variable ...
>>
>> proc create {} {
>> variable ...
>> ...
>> }
>> }
>> ...
>> Board::create
>>
>> Hope that's useful,
>>
>> Simon
>>
>
> It is useful for this newbie who is not the OP.
>
> But I'll nit pick ;/
> Considering that Usenet posts can have a life (approaching infinity) of
> their own, it would be useful if you included counter examples for 1 and
> 2 above. I'll look up the OP's code, but I'm not sure I will see the
> point you are trying to make.

OP had
1) [expr $index + 1]

2) if [allCheck $cellarray($index)] {

Also, your post may outlive the OP's link
> in which case future readers are "up creek ..." ;)
>
> I don't know about others. *BUT* I don't grok
> "" *VS* () *VS* {} *VS* []
>
The differences between these are fundamental and easy to get wrong but
I'll have a go with some examples

set name Bill
puts "My name is $name"
puts {My name is $name}

generates
My name is Bill
My name is $name

normal parentheses are part of the expr parser and used to group
expressions in the normal C style. For example
if {($name eq {bill})} {

square parantheses are used for command evaluation:
proc outputName {arg} {
puts "3: $arg"
return {fred}
}
puts "1: my name is [outputName $name]"
puts {2: my name is [outputName $name]}

generates

3: bill
1: my name is fred
2: my name is [outputName $name]


Simon