1:- module(argutils, 
    2          [	sumargs/2
    3          ,	addargs/3
    4          ]).    5
    6sumargs(Term,Total) :- 
    7   functor(Term,_,N), sumargs(N,Term,0,Total).
    8
    9sumargs(0,_,S,S) :- !.
   10sumargs(N,Term,S1,S3) :- 
   11	succ(M,N), arg(N,Term,X), S2 is S1+X, 
   12	sumargs(M,Term,S2,S3).
 addargs(+Term, +Args, -NewTerm) is det
Add an extra argument to a term, eg
?- addargs(spam(fish,cake),spoon,T).
  T = spam(fish,cake,spoon).
  true.
   23addargs(Term,Args,NewTerm) :-
   24	var(NewTerm) ->
   25		(Term=..L,
   26		append(L,Args,LL),
   27		NewTerm=..LL)
   28	;
   29		(NewTerm=..LL,
   30		append(L,Args,LL),
   31		Term=..L).
   32
   33	%	functor(S1,F,A), arg(N,S1,X1), 
   34	%	functor(S2,F,A), arg(N,S2,X2),
   35	%	phrase(P,X1,X2),
   36	%	foreach((arg(I,S1,X),I\=N), arg(I,S2,X)).
   37
   38% reinstatevars(Bindings,'$VAR'(Name),Var) :- member(Name=Var,Bindings), !. 
   39% reinstatevars(_,Atomic,Atomic) :- atomic(Atomic), !.
   40% reinstatevars(Bindings,Term1,Term2) :-
   41%    Term1 =.. [F | Args1],
   42%    maplist(reinstatevars(Bindings),Args1,Args2),
   43%    Term2 =.. [F | Args2].