1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2010-2011, University of Amsterdam 7 All rights reserved. 8 9 Redistribution and use in source and binary forms, with or without 10 modification, are permitted provided that the following conditions 11 are met: 12 13 1. Redistributions of source code must retain the above copyright 14 notice, this list of conditions and the following disclaimer. 15 16 2. Redistributions in binary form must reproduce the above copyright 17 notice, this list of conditions and the following disclaimer in 18 the documentation and/or other materials provided with the 19 distribution. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 POSSIBILITY OF SUCH DAMAGE. 33*/ 34 35:- module(sicstus_lists, 36 [ substitute/4, % +Elem, +List, +NewElem, -List 37 nth/3, % ?N, ?List, ?Element 38 nth/4, % ?N, ?List, ?Element, ?Rest 39 same_length/3, % ?List1, ?List2, ?Length 40 sublist/2, % ?Sub, +List 41 suffix/2, % ?Suffix, ?List 42 43 % The following predicates are built-in on SWI. 44 % We re-export them here to avoid warnings 45 % when SICStus code explicitly imports them from library(lists). 46 is_list/1, % +Term 47 memberchk/2 % +Element, +List 48 ]). 49:- reexport('../../lists', 50 [ append/3, 51 delete/3, 52 last/2, 53 max_list/2, 54 member/2, 55 min_list/2, 56 nextto/3, 57 nth0/3, 58 nth0/4, 59 permutation/2, 60 prefix/2, 61 reverse/2, 62 same_length/2, 63 select/3, 64 sum_list/2 65 ]). 66 67:- multifile sicstus:rename_module/2. 68 69sicstusrename_module(lists, sicstus_lists). 70 71/** <module> SICStus 3-compatible library(lists). 72 73@tbd This library is incomplete. 74 As of SICStus 3.12.11, the following predicates are missing: 75 76 * no_doubles/1 77 * non_member/2 78 * remove_duplicates/2 79 80@see https://sicstus.sics.se/sicstus/docs/3.12.11/html/sicstus/Lists.html 81*/ 82 83%% substitute(+OldElem, +List, +NewElem, -NewList) is det. 84% 85% NewList is List with all values that are identical (==) to OldElem 86% replaced by NewElem. 87 88substitute(Old, List, New, NewList) :- 89 substitute_(List, Old, New, NewList). 90 91substitute_([], _, _, []). 92substitute_([O|T0], Old, New, [V|T]) :- 93 ( Old == O 94 -> V = New 95 ; V = O 96 ), 97 substitute_(T0, Old, New, T). 98 99 100%% nth(?Index, ?List, ?Element) is nondet. 101% 102% True if Element is the N-th element in List. Counting starts at 103% 1. 104% 105% @deprecated use nth1/3. 106 107nth(Index, List, Element) :- 108 nth1(Index, List, Element). 109 110 111%% nth(?Index, ?List, ?Element, ?Rest) is nondet. 112% 113% True if Element is the N-th element in List and Rest is the 114% remainder (as if by select/3) of List. Counting starts at 1. 115% 116% @deprecated use nth1/4. 117 118nth(Index, List, Element, Rest) :- 119 nth1(Index, List, Element, Rest). 120 121 122%% same_length(?List1, ?List2, ?Length) is nondet. 123% 124% True if List1 and List2 both have length Length. 125 126same_length(List1, List2, Length) :- 127 ( var(List1) 128 -> length(List2, Length), 129 length(List1, Length) 130 ; length(List1, Length), 131 length(List2, Length) 132 ). 133 134 135%% sublist(?Sub, +List) 136% 137% True when all members of Sub are members of List in the same 138% order. 139% 140% @compat sicstus. The order of generating sublists differs. 141% @compat This predicate is known in many Prolog implementations, 142% but the semantics differ. E.g. In YAP it is a continuous 143% sub-list. 144 145sublist(Sub, List) :- 146 sublist_(List, Sub). 147 148sublist_([], []). 149sublist_([H|T], Sub) :- 150 sublist__(T, H, Sub). 151 152sublist__([], H, [H]). 153sublist__([], _, []). 154sublist__([H|T], X, [X|Sub]) :- 155 sublist__(T, H, Sub). 156sublist__([H|T], _, Sub) :- 157 sublist__(T, H, Sub). 158 159 160%% suffix(?Suffix, ?List) is nondet. 161% 162% True if Suffix is a suffix of List. Not the same as suffix/2 163% in SICStus 4 - the arguments are reversed! 164 165suffix(List, List). 166suffix(Suffix, [_|Tail]) :- 167 suffix(Suffix, Tail)