fix nth and nth0 to accept unbound first argument.
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@144 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
parent
bef57eb26c
commit
94271789ef
22
docs/yap.tex
22
docs/yap.tex
@ -6655,7 +6655,7 @@ As @code{member/2}, but may only be used to test whether a known
|
|||||||
@var{Element} occurs in a known Set. In return for this limited use, it
|
@var{Element} occurs in a known Set. In return for this limited use, it
|
||||||
is more efficient when it is applicable.
|
is more efficient when it is applicable.
|
||||||
|
|
||||||
@item nth0(+@var{N}, +@var{List}, ?@var{Elem})
|
@item nth0(?@var{N}, +@var{List}, ?@var{Elem})
|
||||||
@findex nth0/2
|
@findex nth0/2
|
||||||
@syindex nth0/2
|
@syindex nth0/2
|
||||||
@cnindex nth0/2
|
@cnindex nth0/2
|
||||||
@ -6665,14 +6665,14 @@ N elements and unify @var{Elem} with the next.) It can only be used to
|
|||||||
select a particular element given the list and index. For that
|
select a particular element given the list and index. For that
|
||||||
task it is more efficient than @code{member/2}
|
task it is more efficient than @code{member/2}
|
||||||
|
|
||||||
@item nth(+@var{N}, +@var{List}, ?@var{Elem})
|
@item nth(?@var{N}, +@var{List}, ?@var{Elem})
|
||||||
@findex nth/2
|
@findex nth/2
|
||||||
@syindex nth/2
|
@syindex nth/2
|
||||||
@cnindex nth/2
|
@cnindex nth/2
|
||||||
The same as @code{nth0/3}, except that it counts from
|
The same as @code{nth0/3}, except that it counts from
|
||||||
1, that is @code{nth(1, [H|_], H)}.
|
1, that is @code{nth(1, [H|_], H)}.
|
||||||
|
|
||||||
@item nth0(+@var{N}, ?@var{List}, ?@var{Elem}, ?@var{Rest})
|
@item nth0(?@var{N}, ?@var{List}, ?@var{Elem}, ?@var{Rest})
|
||||||
@findex nth0/4
|
@findex nth0/4
|
||||||
@syindex nth0/4
|
@syindex nth0/4
|
||||||
@cnindex nth0/4
|
@cnindex nth0/4
|
||||||
@ -6681,8 +6681,20 @@ counting from 0, and @var{Rest} with the other elements. It can be used
|
|||||||
to select the Nth element of @var{List} (yielding @var{Elem} and @var{Rest}), or to
|
to select the Nth element of @var{List} (yielding @var{Elem} and @var{Rest}), or to
|
||||||
insert @var{Elem} before the Nth (counting from 1) element of @var{Rest}, when
|
insert @var{Elem} before the Nth (counting from 1) element of @var{Rest}, when
|
||||||
it yields @var{List}, e.g. @code{nth0(2, List, c, [a,b,d,e])} unifies List with
|
it yields @var{List}, e.g. @code{nth0(2, List, c, [a,b,d,e])} unifies List with
|
||||||
@code{[a,b,c,d,e]}. nth is the same except that it counts from 1. nth
|
@code{[a,b,c,d,e]}. @code{nth/4} is the same except that it counts from 1. @code{nth0/4}
|
||||||
can be used to insert Elem after the Nth element of Rest.
|
can be used to insert @var{Elem} after the Nth element of @var{Rest}.
|
||||||
|
|
||||||
|
@item nth(?@var{N}, ?@var{List}, ?@var{Elem}, ?@var{Rest})
|
||||||
|
@findex nth/4
|
||||||
|
@syindex nth/4
|
||||||
|
@cnindex nth/4
|
||||||
|
Unifies @var{Elem} with the Nth element of @var{List}, counting from 1,
|
||||||
|
and @var{Rest} with the other elements. It can be used to select the
|
||||||
|
Nth element of @var{List} (yielding @var{Elem} and @var{Rest}), or to
|
||||||
|
insert @var{Elem} before the Nth (counting from 1) element of
|
||||||
|
@var{Rest}, when it yields @var{List}, e.g. @code{nth(1, List, c,
|
||||||
|
[a,b,d,e])} unifies List with @code{[a,b,c,d,e]}. @code{nth/4}
|
||||||
|
can be used to insert @var{Elem} after the Nth element of @var{Rest}.
|
||||||
|
|
||||||
@item permutation(+@var{List},?@var{Perm})
|
@item permutation(+@var{List},?@var{Perm})
|
||||||
@findex permutation/2
|
@findex permutation/2
|
||||||
|
@ -100,7 +100,7 @@ nextto(X,Y, [X,Y|_]).
|
|||||||
nextto(X,Y, [_|List]) :-
|
nextto(X,Y, [_|List]) :-
|
||||||
nextto(X,Y, List).
|
nextto(X,Y, List).
|
||||||
|
|
||||||
% nth0(+N, +List, ?Elem) is true when Elem is the Nth member of List,
|
% nth0(?N, +List, ?Elem) is true when Elem is the Nth member of List,
|
||||||
% counting the first as element 0. (That is, throw away the first
|
% counting the first as element 0. (That is, throw away the first
|
||||||
% N elements and unify Elem with the next.) It can only be used to
|
% N elements and unify Elem with the next.) It can only be used to
|
||||||
% select a particular element given the list and index. For that
|
% select a particular element given the list and index. For that
|
||||||
@ -108,29 +108,45 @@ nextto(X,Y, [_|List]) :-
|
|||||||
% nth(+N, +List, ?Elem) is the same as nth0, except that it counts from
|
% nth(+N, +List, ?Elem) is the same as nth0, except that it counts from
|
||||||
% 1, that is nth(1, [H|_], H).
|
% 1, that is nth(1, [H|_], H).
|
||||||
|
|
||||||
|
nth0(V, In, Element) :- var(V), !,
|
||||||
|
generate_nth0(V, In, Element).
|
||||||
nth0(0, [Head|_], Head) :- !.
|
nth0(0, [Head|_], Head) :- !.
|
||||||
|
|
||||||
nth0(N, [_|Tail], Elem) :-
|
nth0(N, [_|Tail], Elem) :-
|
||||||
nonvar(N), !,
|
|
||||||
M is N-1,
|
M is N-1,
|
||||||
nth0(M, Tail, Elem).
|
find_nth0(M, Tail, Elem).
|
||||||
|
|
||||||
nth0(N,[_|T],Item) :- % Clause added KJ 4-5-87 to allow mode
|
find_nth0(0, [Head|_], Head) :- !.
|
||||||
nth0(M,T,Item),
|
find_nth0(N, [_|Tail], Elem) :-
|
||||||
N is M + 1.
|
M is N-1,
|
||||||
|
find_nth0(M, Tail, Elem).
|
||||||
|
|
||||||
|
|
||||||
|
generate_nth0(0, [Head|_], Head).
|
||||||
|
generate_nth0(I, [_|List], El) :-
|
||||||
|
generate_nth0(I1, List, El),
|
||||||
|
I is I1+1.
|
||||||
|
|
||||||
|
|
||||||
|
nth(V, In, Element) :- var(V), !,
|
||||||
|
generate_nth(V, In, Element).
|
||||||
nth(1, [Head|_], Head) :- !.
|
nth(1, [Head|_], Head) :- !.
|
||||||
|
|
||||||
nth(N, [_|Tail], Elem) :-
|
nth(N, [_|Tail], Elem) :-
|
||||||
nonvar(N), !,
|
nonvar(N), !,
|
||||||
M is N-1, % should be succ(M, N)
|
M is N-1, % should be succ(M, N)
|
||||||
nth(M, Tail, Elem).
|
find_nth(M, Tail, Elem).
|
||||||
|
|
||||||
|
find_nth(1, [Head|_], Head) :- !.
|
||||||
|
find_nth(N, [_|Tail], Elem) :-
|
||||||
|
M is N-1,
|
||||||
|
find_nth(M, Tail, Elem).
|
||||||
|
|
||||||
|
|
||||||
|
generate_nth(1, [Head|_], Head).
|
||||||
|
generate_nth(I, [_|List], El) :-
|
||||||
|
generate_nth(I1, List, El),
|
||||||
|
I is I1+1.
|
||||||
|
|
||||||
|
|
||||||
nth(N,[_|T],Item) :- % Clause added KJ 4-5-87 to allow mode
|
|
||||||
% nth(-,+,+)
|
|
||||||
nth(M,T,Item),
|
|
||||||
N is M + 1.
|
|
||||||
|
|
||||||
% nth0(+N, ?List, ?Elem, ?Rest) unifies Elem with the Nth element of List,
|
% nth0(+N, ?List, ?Elem, ?Rest) unifies Elem with the Nth element of List,
|
||||||
% counting from 0, and Rest with the other elements. It can be used
|
% counting from 0, and Rest with the other elements. It can be used
|
||||||
@ -140,30 +156,44 @@ nth(N,[_|T],Item) :- % Clause added KJ 4-5-87 to allow mode
|
|||||||
% [a,b,c,d,e]. nth is the same except that it counts from 1. nth
|
% [a,b,c,d,e]. nth is the same except that it counts from 1. nth
|
||||||
% can be used to insert Elem after the Nth element of Rest.
|
% can be used to insert Elem after the Nth element of Rest.
|
||||||
|
|
||||||
|
nth0(V, In, Element, Tail) :- var(V), !,
|
||||||
|
generate_nth0(V, In, Element, Tail).
|
||||||
nth0(0, [Head|Tail], Head, Tail) :- !.
|
nth0(0, [Head|Tail], Head, Tail) :- !.
|
||||||
|
|
||||||
nth0(N, [Head|Tail], Elem, [Head|Rest]) :-
|
nth0(N, [Head|Tail], Elem, [Head|Rest]) :-
|
||||||
nonvar(N),
|
|
||||||
M is N-1,
|
M is N-1,
|
||||||
nth0(M, Tail, Elem, Rest).
|
nth0(M, Tail, Elem, Rest).
|
||||||
|
|
||||||
nth0(N, [Head|Tail], Elem, [Head|Rest]) :- % Clause added KJ 4-5-87
|
find_nth0(0, [Head|Tail], Head, Tail) :- !.
|
||||||
var(N), % to allow mode
|
find_nth0(N, [Head|Tail], Elem, [Head|Rest]) :-
|
||||||
nth0(M, Tail, Elem, Rest), % nth0(-,+,+,?).
|
M is N-1,
|
||||||
N is M+1.
|
find_nth0(M, Tail, Elem, Rest).
|
||||||
|
|
||||||
|
|
||||||
|
generate_nth0(0, [Head|Rest], Head, Rest).
|
||||||
|
generate_nth0(I, [Head|List], El, [Head|Rest]) :-
|
||||||
|
generate_nth0(I1, List, El, Rest),
|
||||||
|
I is I1+1.
|
||||||
|
|
||||||
|
|
||||||
|
nth(V, In, Element, Tail) :- var(V), !,
|
||||||
|
generate_nth(V, In, Element, Tail).
|
||||||
nth(1, [Head|Tail], Head, Tail) :- !.
|
nth(1, [Head|Tail], Head, Tail) :- !.
|
||||||
|
|
||||||
nth(N, [Head|Tail], Elem, [Head|Rest]) :-
|
nth(N, [Head|Tail], Elem, [Head|Rest]) :-
|
||||||
nonvar(N),
|
|
||||||
M is N-1,
|
M is N-1,
|
||||||
nth(M, Tail, Elem, Rest).
|
nth(M, Tail, Elem, Rest).
|
||||||
|
|
||||||
nth(N, [Head|Tail], Elem, [Head|Rest]) :- % Clause added KJ 4-5-87
|
find_nth(1, [Head|Tail], Head, Tail) :- !.
|
||||||
var(N), % to allow mode
|
find_nth(N, [Head|Tail], Elem, [Head|Rest]) :-
|
||||||
nth(M, Tail, Elem, Rest), % nth(-,+,+,?).
|
M is N-1,
|
||||||
N is M+1.
|
find_nth(M, Tail, Elem, Rest).
|
||||||
|
|
||||||
|
|
||||||
|
generate_nth(1, [Head|Rest], Head, Rest).
|
||||||
|
generate_nth(I, [Head|List], El, [Head|Rest]) :-
|
||||||
|
generate_nth(I1, List, El, Rest),
|
||||||
|
I is I1+1.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
% permutation(List, Perm)
|
% permutation(List, Perm)
|
||||||
|
Reference in New Issue
Block a user