List operators
Operators can be used to manipulate lists. The List operators are the following:
returns a sublist of a given list using zero-based indexes denoting the start and the end of the sublist |
|
returns true if a given list contains a specified element |
|
returns true if the second list is a prefix of the first list |
|
returns true if a given list ends with a specified suffix |
|
returns the concatenation of two lists |
|
works as list concatenation if any of the two operands is a list |
|
returns the length of a given list |
substring
The ternary operator substring(List, Start, End)
returns the sublist of List
using zero-based indexes Start
and End
, which denote, respectively, the start (inclusive) and the end (exclusive) of the returned sublist.
substring(List, Start, End)
Where:
-
List
is the list from which to extract the sublist. -
Start
is the zero-based starting index (inclusive). -
End
is the zero-based ending index (exclusive).
a([0, 1, 2, 3, 4, 5], 1, 3).
b(P) :- a(X, Y, Z), P = substring(X, Y, Z).
@output("b").
b([1, 2])
contains
The Boolean list operator contains(List, Element)
returns true if List
contains Element
.
contains(List, Element)
Where:
-
List
is the list to be checked. -
Element
is the element to check for in the list.
a([0, 1, 2, 3, 4, 5]).
b(3).
b(2).
c(Y, J) :- a(X), b(Y), J = contains(X, Y).
@output("c").
c(3, #T)
c(2, #F)
starts_with
The Boolean list operator starts_with(List, Prefix)
returns true if the second list is a prefix of the first.
starts_with(List, Prefix)
Where:
-
List
is the list to be checked. -
Prefix
is the prefix to check for in the list.
a([0, 1, 2, 3, 4, 5]).
b([0, 1]).
b([0, 1, 3]).
c(Y, J) :- a(X), b(Y), J = starts_with(X, Y).
@output("c").
c([0, 1], #T)
c([0, 1, 3], #F)
ends_with
The Boolean operator ends_with(List, Suffix)
returns true if List
ends with Suffix
.
ends_with(List, Suffix)
Where:
-
List
is the list to be checked. -
Suffix
is the suffix to check for in the list.
a([0, 1, 2, 3, 4, 5]).
b([4, 5]).
b([2, 4, 5]).
c(Y, J) :- a(X), b(Y), J = ends_with(X, Y).
@output("c").
c([4, 5], #T)
c([2, 4, 5], #F)
concat
The binary list operator concat(List1, List2)
returns the concatenation of List1
and List2
. If only one of the operands is a list, the other operand is treated as a list of one element.
concat(List1, List2)
Where:
-
List1
is the first list. -
List2
is the second list.
a(["a", "b"], [1, 2]).
a(["a", "b"], "c").
c(J) :- a(X, Y), J = concat(X, Y).
@output("c").
c(["a", "b", 1, 2])
c(["a", "b", "c"])
+
(plus symbol)
If any of the two operands is a list, then the operation +
works as list concat
.
List1 + List2
Where:
-
List1
is the first list or element. -
List2
is the second list or element.
a(["a", "b"], [1, 2]).
a(["a", "b"], "c").
c(J) :- a(X, Y), J = X + Y.
@output("c").
c(["a", "b", 1, 2])
c(["a", "b", "c"])