child
Thechild
filter returns the primary child of the current position. Thus,
x=childwill set the variable
x
to the primary child of the current position (if the current position is a terminal position, then the assignment will not match and x
will not be modified). Recall that if variations is not set in the CQL header, then the primary child of the current position is the same as the child, as any position has at most one child.
If the child
filter is followed by a numeric filter enclosed in parentheses, then the the child with index i is returned, where i is the value of that numeric filter:
x=child(1)If no such child exists, the filter will not match. Thus
child
without an argument is the same as child(0)
.
Getting the number of children
You can use the following function to compute the number of children of the current position:function nchildren(){ nChildrenVal=0 if loop{ child(nChildrenVal) nChildrenVal+=1 } then nChildrenVal else 0}
After inserting the above into a CQL file, the expression nchildren()
has as value the number of children in the current position.
The loop here
will loop endlessly until its body fails, which can only happen when child(nChildrenVal)
fails. At that point, nChildrenVal
has value of the number of children of the current position. The if/then/else
circumlocution is necessary because loop will not match if its body never matched.
Examples
Thechild
filter is used in the examples: knightpawnforkecho.cql, parallelpaths.cql, and queenpawnpinecho.cql.
In parallelpaths.cql for instance, the variable current1
holds a position. The expression
current1=current:childsets
current1
to its child, if it exists (and fails to match if the child does not exist, that is, if current1
is terminal)