View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    WWW:           http://www.swi-prolog.org
    4    Copyright (c)  2020-2021, SWI-Prolog Solutions b.v.
    5    All rights reserved.
    6
    7    Redistribution and use in source and binary forms, with or without
    8    modification, are permitted provided that the following conditions
    9    are met:
   10
   11    1. Redistributions of source code must retain the above copyright
   12       notice, this list of conditions and the following disclaimer.
   13
   14    2. Redistributions in binary form must reproduce the above copyright
   15       notice, this list of conditions and the following disclaimer in
   16       the documentation and/or other materials provided with the
   17       distribution.
   18
   19    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   20    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   21    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   22    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   23    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   24    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   25    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   26    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   27    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   29    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   30    POSSIBILITY OF SUCH DAMAGE.
   31*/
   32
   33:- module(sicstus4_lists,
   34	  [ keys_and_values/3,		% ?Pairs, ?Keys, ?Values
   35	    rev/2,			% +List, ?Reversed
   36	    shorter_list/2,		% ?Short, ?Long
   37	    append_length/4,		% ?Prefix, ?Suffix, ?List, ?Length
   38	    append_length/3,		% ?Suffix, ?List, ?Length
   39	    prefix_length/3,		% ?List, ?Prefix, ?Length
   40	    proper_prefix_length/3,	% ?List, ?Prefix, ?Length
   41	    suffix_length/3,		% ?List, ?Suffix, ?Length
   42	    proper_suffix_length/3,	% ?List, ?Suffix, ?Length
   43	    sublist/5,			% +Whole, ?Part, ?Before, ?Length, ?After
   44	    sublist/4,			% +Whole, ?Part, ?Before, ?Length
   45	    sublist/3,			% +Whole, ?Part, ?Before
   46	    cons/3,			% ?Head, ?Tail, ?List
   47	    last/3,			% ?Fore, ?Last, ?List
   48	    head/2,			% ?List, ?Head
   49	    tail/2,			% ?List, ?Tail
   50	    prefix/2,			% ?List, ?Prefix
   51	    proper_prefix/2,		% ?List, ?Prefix
   52	    suffix/2,			% ?List, ?Suffix
   53	    proper_suffix/2,		% ?List, ?Suffix
   54	    subseq0/2,			% +Sequence, ?SubSequence
   55	    subseq1/2,			% +Sequence, ?SubSequence
   56	    scanlist/4,			% :Pred, ?Xs, ?V1, ?V
   57	    scanlist/5,			% :Pred, ?Xs, ?Ys, ?V1, ?V
   58	    scanlist/6,			% :Pred, ?Xs, ?Ys, ?Zs, ?V1, ?V
   59	    
   60	    % is_list/1 is built-in on SWI.
   61	    % We re-export it here to avoid warnings
   62	    % when SICStus code explicitly imports it from library(lists).
   63	    is_list/1			% +Term
   64	  ]).   65:- reexport('../../lists',
   66	    [ select/3,
   67	      selectchk/3,
   68	      append/2,
   69	      delete/3,
   70	      last/2,
   71	      % SWI lists:list_to_set/2 actually behaves
   72	      % like SICStus lists:remove_dups/2
   73	      % and not like SICStus sets:list_to_set/2.
   74	      list_to_set/2 as remove_dups,
   75	      nextto/3,
   76	      nth1/3,
   77	      nth1/4,
   78	      nth0/3,
   79	      nth0/4,
   80	      permutation/2,
   81	      proper_length/2,
   82	      reverse/2,
   83	      same_length/2,
   84	      select/4,
   85	      selectchk/4,
   86	      sum_list/2 as sumlist,
   87	      max_member/2,
   88	      min_member/2,
   89	      clumped/2
   90	    ]).   91:- reexport('../../apply',
   92	    [ maplist/2,
   93	      maplist/3,
   94	      maplist/4,
   95	      convlist/3,
   96	      exclude/3,
   97	      include/3,
   98	      partition/5
   99	    ]).  100:- reexport('../../clp/clpfd', [transpose/2]).  101:- reexport('../sicstus/lists', [same_length/3]).  102:- use_module(library(pairs), [pairs_keys_values/3]).  103
  104:- multifile sicstus4:rename_module/2.  105
  106sicstus4:rename_module(lists, sicstus4_lists).

SICStus 4-compatible library(lists).

See also
- https://sicstus.sics.se/sicstus/docs/4.6.0/html/sicstus.html/lib_002dlists.html */
To be done
- This library is incomplete. As of SICStus 4.6.0, the following predicates are missing:
  • append/5
  • correspond/4
  • delete/4
  • one_longer/2
  • perm/2
  • perm2/4
  • rotate_list/[2,3]
  • segment/2
  • proper_segment/2
  • cumlist/[4,5,6]
  • map_product/4
  • some/[2,3,4]
  • somechk/[2,3,4]
  • exclude/[4,5]
  • include/[4,5]
  • group/[3,4,5]
  • ordered/[1,2]
  • max_member/3
  • min_member/3
  • select_min/[3,4]
  • select_max/[3,4]
  • increasing_prefix/[3,4]
  • decreasing_prefix/[3,4]
  • clumps/2
  • keyclumps/2
  • keyclumped/2
  143keys_and_values(Pairs, Keys, Values) :-
  144	pairs_keys_values(Pairs, Keys, Values).
 rev(+List, ?Reversed) is semidet
Same as reverse/2, but List must be a proper list.
  151rev(List, Reversed) :- rev_(List, [], Reversed).
  152rev_([], Reversed, Reversed).
  153rev_([Head|Tail], RevTail, Reversed) :-
  154	rev_(Tail, [Head|RevTail], Reversed).
 shorter_list(?Short, ?Long) is nondet
True if Short is a shorter list than Long. The lists' contents are insignificant, only the lengths matter. Mode -Short, +Long can be used to enumerate list skeletons shorter than Long.
  163shorter_list([], [_|_]).
  164shorter_list([_|ShortTail], [_|LongTail]) :-
  165	shorter_list(ShortTail, LongTail).
  166
  167
  168% TODO The *_length predicates can probably be implemented more efficiently.
  169
  170append_length(Prefix, Suffix, List, Length) :-
  171	append(Prefix, Suffix, List),
  172	length(Prefix, Length).
  173
  174append_length(Suffix, List, Length) :-
  175	append_length(_, Suffix, List, Length).
  176
  177prefix_length(List, Prefix, Length) :-
  178	prefix(List, Prefix),
  179	length(Prefix, Length).
  180
  181proper_prefix_length(List, Prefix, Length) :-
  182	proper_prefix(List, Prefix),
  183	length(Prefix, Length).
  184
  185suffix_length(List, Suffix, Length) :-
  186	suffix(List, Suffix),
  187	length(Suffix, Length).
  188
  189proper_suffix_length(List, Suffix, Length) :-
  190	proper_suffix(List, Suffix),
  191	length(Suffix, Length).
  192
  193
  194sublist(Whole, Part, Before, Length, After) :-
  195	append(Prefix, Tail, Whole),
  196	append(Part, Suffix, Tail),
  197	length(Prefix, Before),
  198	length(Part, Length),
  199	length(Suffix, After).
  200
  201sublist(Whole, Part, Before, Length) :-
  202	sublist(Whole, Part, Before, Length, _).
  203
  204sublist(Whole, Part, Before) :-
  205	sublist(Whole, Part, Before, _, _).
  206
  207
  208cons(Head, Tail, [Head|Tail]).
  209last(Fore, Last, List) :- append(Fore, [Last], List).
  210head([Head|_], Head).
  211tail([_|Tail], Tail).
 prefix(?List, ?Prefix) is nondet
True if Prefix is a prefix of List. Not the same as prefix/2 in SICStus 3 or SWI - the arguments are reversed!
  219prefix(List, Prefix) :-
  220	append(Prefix, _, List).
 proper_prefix(?List, ?Prefix) is nondet
True if Prefix is a prefix of List, but is not List itself.
  226proper_prefix(List, Prefix) :-
  227	prefix(List, Prefix),
  228	Prefix \== List.
 suffix(?List, ?Prefix) is nondet
True if Suffix is a suffix of List. Not the same as suffix/2 in SICStus 3 - the arguments are reversed!
  235suffix(List, List).
  236suffix([_|Tail], Suffix) :-
  237	suffix(Tail, Suffix).
 proper_suffix(?List, ?Prefix) is nondet
True if Suffix is a suffix of List, but is not List itself.
  243proper_suffix([_|Tail], Suffix) :-
  244	suffix(Tail, Suffix).
 scanlist(:Pred, ?Xs, ?V1, ?V) is nondet
 scanlist(:Pred, ?Xs, ?Ys, ?V1, ?V) is nondet
 scanlist(:Pred, ?Xs, ?Ys, ?Zs, ?V1, ?V) is nondet
Same as foldl/[4,5,6].
Compatibility
- SICStus 4
  255:- meta_predicate scanlist(3, ?, ?, ?).  256scanlist(Pred, Xs, V1, V) :- foldl(Pred, Xs, V1, V).
  257:- meta_predicate scanlist(4, ?, ?, ?, ?).  258scanlist(Pred, Xs, Ys, V1, V) :- foldl(Pred, Xs, Ys, V1, V).
  259:- meta_predicate scanlist(5, ?, ?, ?, ?, ?).  260scanlist(Pred, Xs, Ys, Zs, V1, V) :- foldl(Pred, Xs, Ys, Zs, V1, V).
  261
  262
  263subseq(Sequence, [], Sequence).
  264subseq([Head|Tail], [Head|SubTail], Complement) :-
  265	subseq(Tail, SubTail, Complement).
  266subseq([Head|Tail], SubSequence, [Head|Complement]) :-
  267	subseq(Tail, SubSequence, Complement).
  268
  269
  270subseq0(Sequence, SubSequence) :- subseq(Sequence, SubSequence, _).
  271subseq1(Sequence, SubSequence) :-
  272	subseq(Sequence, SubSequence, Complement),
  273	Complement \== []