nb_setarg/3

"nb_setarg(+{Arg], + Term, + Value" )

Assigns the _Arg_-th argument of the compound term _Term_ with the given _Value_ as @ref setarg_51 @"setarg/3", but on backtracking the assignment is not reversed. If _Term_ is not atomic, it is duplicated using @ref duplicate_term_50 @"duplicate_term/2". This predicate uses the same technique as @ref nb_setval_50 @"nb_setval/2". We therefore refer to the description of @ref nb_setval_50 @"nb_setval/2" for details on non-backtrackable assignment of terms. This predicate is compatible to GNU-Prologsetarg(A,T,V,false)`, removing the type-restriction on Value. See also @ref nb_linkarg_51 @"nb_linkarg/3". Below is an example for counting the number of solutions of a goal. Note that this implementation is thread-safe, reentrant and capable of handling exceptions. Realising these features with a traditional implementation based on assert/retract or flag/3 is much more complicated.

```
succeeds_n_times(Goal, Times) :-
Counter = counter(0),
(   Goal,
arg(1, Counter, N0),
N is N0 + 1,
nb_setarg(1, Counter, N),
fail
;   arg(1, Counter, Times)
).
```

`