From 94271789ef743313deeb9ae49cad2fadd64b6f5d Mon Sep 17 00:00:00 2001 From: vsc Date: Mon, 27 Aug 2001 15:27:29 +0000 Subject: [PATCH] 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 --- docs/yap.tex | 22 ++++++++++--- library/lists.yap | 80 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 72 insertions(+), 30 deletions(-) diff --git a/docs/yap.tex b/docs/yap.tex index 30d646005..9bcdfd701 100644 --- a/docs/yap.tex +++ b/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 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 @syindex 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 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 @syindex nth/2 @cnindex nth/2 The same as @code{nth0/3}, except that it counts from 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 @syindex 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 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 -@code{[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. +@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 @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}) @findex permutation/2 diff --git a/library/lists.yap b/library/lists.yap index 9015b1912..2d7fb8719 100644 --- a/library/lists.yap +++ b/library/lists.yap @@ -100,7 +100,7 @@ nextto(X,Y, [X,Y|_]). 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 % 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 @@ -108,29 +108,45 @@ nextto(X,Y, [_|List]) :- % nth(+N, +List, ?Elem) is the same as nth0, except that it counts from % 1, that is nth(1, [H|_], H). +nth0(V, In, Element) :- var(V), !, + generate_nth0(V, In, Element). nth0(0, [Head|_], Head) :- !. - nth0(N, [_|Tail], Elem) :- - nonvar(N), !, 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 - nth0(M,T,Item), - N is M + 1. +find_nth0(0, [Head|_], Head) :- !. +find_nth0(N, [_|Tail], Elem) :- + 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(N, [_|Tail], Elem) :- nonvar(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, % 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 % 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(N, [Head|Tail], Elem, [Head|Rest]) :- - nonvar(N), M is N-1, nth0(M, Tail, Elem, Rest). -nth0(N, [Head|Tail], Elem, [Head|Rest]) :- % Clause added KJ 4-5-87 - var(N), % to allow mode - nth0(M, Tail, Elem, Rest), % nth0(-,+,+,?). - N is M+1. +find_nth0(0, [Head|Tail], Head, Tail) :- !. +find_nth0(N, [Head|Tail], Elem, [Head|Rest]) :- + M is N-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(N, [Head|Tail], Elem, [Head|Rest]) :- - nonvar(N), M is N-1, nth(M, Tail, Elem, Rest). -nth(N, [Head|Tail], Elem, [Head|Rest]) :- % Clause added KJ 4-5-87 - var(N), % to allow mode - nth(M, Tail, Elem, Rest), % nth(-,+,+,?). - N is M+1. +find_nth(1, [Head|Tail], Head, Tail) :- !. +find_nth(N, [Head|Tail], Elem, [Head|Rest]) :- + M is N-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)