0

Am attempting to teach myself to program using Tcl. (I want to become more familiar with the language to understand someone else's code - SCID chess) The task i've set myself to motivate my learing of Tcl is to solve the 8 queens problem. My approach to creating a program is to sucessively 'prototype' a solution. So. I'm up to nesting a for loop holding the q pos on row 2 inside the for loop holding the q pos on row 1

Here is my code

set allowd 1
set notallowd 0

for {set r1p 1} {$r1p <= 8} {incr r1p } {
    puts "1st row q placed at $r1p" 
    ;# re-initialize r2 'free for q placemnt' array after every change of r1 q pos:
    for {set i 1 } {$i <= 8} {incr i} { set r2($i) $allowd    }

    for { set r2($r1p) $notallowd ; set r2([eval $r1p-1]) $notallowd ;
           set r2([eval $r1p+1]) $notallowd ; set r2p 1}   {$r2p <= 8} {
         incr r2p ;# end of 'next' arg of r2 forloop
        }
        ;# commnd arg of r2 forloop placed below: 
        {puts "2nd row q placed at $r2p"    
    }
} 

My problem is that when i run the code the interpreter is aborting with the fatal error: "wrong #args should be for start test next command.

I've gone over my code a few times and can't see that i've missed any of the for loop arguments.

1 Answer 1

5

The carriage return before the command in the last for loop is what's getting you. From the first syntax rule on the Tcl man page, "Semi-colons and newlines are command separators unless quoted as described below." BTW, your eval's should be expr's.

This works for me:

set allowd 1
set notallowd 0

for {set r1p 1} {$r1p <= 8} {incr r1p } {
    puts "1st row q placed at $r1p" 
    ;# re-initialize r2 'free for q placemnt' array after every change of r1 q pos:
    for {set i 1 } {$i <= 8} {incr i} { set r2($i) $allowd    }

    for { set r2($r1p) $notallowd ; set r2([expr $r1p-1]) $notallowd ;
        set r2([expr $r1p+1]) $notallowd ; set r2p 1}   {$r2p <= 8} {
        incr r2p ;# end of 'next' arg of r2 forloop
    } {
        # commnd arg of r2 forloop placed below: 
        puts "2nd row q placed at $r2p"    
    }
} 
Sign up to request clarification or add additional context in comments.

3 Comments

thanks very much! Will this work as an aide memoire for me not to fall down the same type of error again: if one runs out of space and arguments remain to be typed for a function then an opening brace for the next argument must be placed at the end of the line , to give warning (so to say) to the interpreter that 'more is to follow' ?
It's not that the interpreter changes how it parses the command, it's that the arg's body happens to contain a newline. BTW, you can also continue a command by adding "\" at the end of the line.
To be exact, the Tcl interpreter (and also the bytecode compiler, of course) always parses everything in exactly the same way. Really. There aren't any special syntaxes that aren't listed on the main Tcl language manual page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.