Continue - Maple Help

 Continue

 Calling Sequence Continue(fcn, arg1, ..., argN )

Parameters

 fcn - (appliable) the function to execute as the continuation task args1..argsN - (anything) an argument to fcn or the specification for a child task

Description

 • The Continue function creates zero or more child tasks and a continuation task.
 • The Continue function is part of the Task Programming Model.
 • Continue behaves much like the Start function, with two important differences.  First Continue does not wait for the tasks it creates to complete, instead Continue returns as soon as the tasks are created. Secondly, Continue may only be called from within a invocation of the Task Model.  The Continue function is intended to spread work across multiple tasks as opposed to calculate a particular result.
 • If child tasks are specified, then Continue creates one task for each child, and a continuation task with fnc as the continuation function. The value of fnc is either a procedure or the name of a predefined continuation function.
 • The two predefined continuation functions are passed and null
 passed returns the arguments that were passed into it.
 null returns NULL.
 • An argument to Continue of the form
 Task = [ cfcn, carg1 ... cargn ]
 creates a child task that executes the following command.
 cfcn( carg1 ... cargn )
 • An argument to Continue of the form:
 Tasks = [ cfcn, [c1args] ... [cnargs] ]
 behaves as if multiple Task arguments of the form
 Task = [ cfcn, c1args ], Task = [ cfcn, c2args ], ... Task = [ cfcn, cnargs ]
 had been given.  Thus, it creates tasks to execute the following commands.
 cfcn( c1args ), cfcn( c2args ) ... cfcn( cnargs )
 • The return value of a child task is passed to the continuation function in the child's position in its argument sequence.
 • Arguments not of the form described above are passed to the continuation function fcn as arguments in the corresponding positions.
 • If child tasks are created as part of the call to Continue, the continuation task will not start executing until all of its child tasks have completed.

Examples

Solve the N Queens problem: place N queens on an N X N chess board so that no queen can capture any other queen.

 > nQueens := module()     local checkBoard,             completeBoardAndCheck,             search,             continuation,             subInit;     export ModuleApply;     checkBoard := proc( n, board::Array )         local i, j, index;         for i from 1 to n-1         do            index := board[i]+1;            for j from i+1 to n while index <= n            do                if ( index = board[j] ) then                    return NULL;                end;                index := index + 1;            end do;            index := board[i] - 1;            for j from i+1 to n while index >= 0            do                if ( index = board[j] ) then                    return NULL;                end;                index := index - 1;            end do;        end do;        return Array( board );    end proc;    completeBoardAndCheck := proc( n, board, i, unused )        local j;        if ( i < n ) then            return op( map( proc( j )                            board[i] := j;                            completeBoardAndCheck( n, board, i+1,                                unused minus {j} )                        end proc, unused ) );        else            board[n] := unused[1];            return checkBoard( n, board );        end if;    end proc;    continuation := proc()        args;    end proc;    search := proc( i::posint, n::posint, m::nonnegint, board::Array )        local j, k, boards, a, used, unused;        if ( i <= m ) then            if ( i > 1 ) then                used := convert( board[1..i-1], set );                boards := [ seq( Array( board ), k=1..n-i+1 ) ];                k := 1;                for j from 1 to n                do                    if ( not j in used ) then                        boards[k][i] := j;                        k := k+1;                    end if;                end do;            else                boards := [ seq( Array( board ), k=1..n ) ];                for j from 1 to n                do                    boards[j][i] := j;                end do;            end if;            Threads:-Task:-Continue( continuation,                Tasks = [ search, seq( [i+1, n, m, j], j in boards ) ] );        else            unused := { seq( j, j=1..n ) } minus convert( board[1..i-1], set );            return completeBoardAndCheck( n, board, i, unused );        end if;        return NULL;    end proc;    ModuleApply := proc( n::posint, m::nonnegint )        local board;        board := Array( 1..n, datatype=integer[8] );        Threads:-Task:-Start( search, 1, n, m, board );    end proc; end;
 ${\mathrm{nQueens}}{≔}{\mathbf{module}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{checkBoard}}{,}{\mathrm{completeBoardAndCheck}}{,}{\mathrm{search}}{,}{\mathrm{continuation}}{,}{\mathrm{subInit}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{export}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{ModuleApply}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (1)
 > $\mathrm{boards}≔\left[\mathrm{nQueens}\left(1,0\right)\right]$
 ${\mathrm{boards}}{≔}\left[\left[\begin{array}{c}{1}\end{array}\right]\right]$ (2)
 > $\mathrm{nops}\left(\mathrm{boards}\right)$
 ${1}$ (3)
 > $\mathrm{boards}≔\left[\mathrm{nQueens}\left(2,1\right)\right]$
 ${\mathrm{boards}}{≔}\left[\right]$ (4)
 > $\mathrm{nops}\left(\mathrm{boards}\right)$
 ${0}$ (5)
 > $\mathrm{boards}≔\left[\mathrm{nQueens}\left(3,1\right)\right]$
 ${\mathrm{boards}}{≔}\left[\right]$ (6)
 > $\mathrm{nops}\left(\mathrm{boards}\right)$
 ${0}$ (7)
 > $\mathrm{boards}≔\left[\mathrm{nQueens}\left(4,2\right)\right]$
 ${\mathrm{boards}}{≔}\left[\left[\begin{array}{cccc}{2}& {4}& {1}& {3}\end{array}\right]{,}\left[\begin{array}{cccc}{3}& {1}& {4}& {2}\end{array}\right]\right]$ (8)
 > $\mathrm{nops}\left(\mathrm{boards}\right)$
 ${2}$ (9)
 > $\mathrm{boards}≔\left[\mathrm{nQueens}\left(5,3\right)\right]$
 ${\mathrm{boards}}{≔}\left[\left[\begin{array}{ccccc}{1}& {3}& {5}& {2}& {4}\end{array}\right]{,}\left[\begin{array}{ccccc}{1}& {4}& {2}& {5}& {3}\end{array}\right]{,}\left[\begin{array}{ccccc}{2}& {4}& {1}& {3}& {5}\end{array}\right]{,}\left[\begin{array}{ccccc}{2}& {5}& {3}& {1}& {4}\end{array}\right]{,}\left[\begin{array}{ccccc}{3}& {1}& {4}& {2}& {5}\end{array}\right]{,}\left[\begin{array}{ccccc}{3}& {5}& {2}& {4}& {1}\end{array}\right]{,}\left[\begin{array}{ccccc}{4}& {1}& {3}& {5}& {2}\end{array}\right]{,}\left[\begin{array}{ccccc}{4}& {2}& {5}& {3}& {1}\end{array}\right]{,}\left[\begin{array}{ccccc}{5}& {2}& {4}& {1}& {3}\end{array}\right]{,}\left[\begin{array}{ccccc}{5}& {3}& {1}& {4}& {2}\end{array}\right]\right]$ (10)
 > $\mathrm{nops}\left(\mathrm{boards}\right)$
 ${10}$ (11)
 > $\mathrm{boards}≔\left[\mathrm{nQueens}\left(6,4\right)\right]$
 ${\mathrm{boards}}{≔}\left[\left[\begin{array}{cccccc}{2}& {4}& {6}& {1}& {3}& {5}\end{array}\right]{,}\left[\begin{array}{cccccc}{3}& {6}& {2}& {5}& {1}& {4}\end{array}\right]{,}\left[\begin{array}{cccccc}{4}& {1}& {5}& {2}& {6}& {3}\end{array}\right]{,}\left[\begin{array}{cccccc}{5}& {3}& {1}& {6}& {4}& {2}\end{array}\right]\right]$ (12)
 > $\mathrm{nops}\left(\mathrm{boards}\right)$
 ${4}$ (13)
 > $\mathrm{boards}≔\left[\mathrm{nQueens}\left(7,4\right)\right]:$
 > $\mathrm{nops}\left(\mathrm{boards}\right)$
 ${40}$ (14)
 > $\mathrm{boards}≔\left[\mathrm{nQueens}\left(8,4\right)\right]:$
 > $\mathrm{nops}\left(\mathrm{boards}\right)$
 ${92}$ (15)