List operators

Operators can be used to manipulate lists. The List operators are the following:

substring

returns a sublist of a given list using zero-based indexes denoting the start and the end of the sublist

contains

returns true if a given list contains a specified element

starts_with

returns true if the second list is a prefix of the first list

ends_with

returns true if a given list ends with a specified suffix

concat

returns the concatenation of two lists

+ (plus symbol)

works as list concatenation if any of the two operands is a list

string_length

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).

Example
a([0, 1, 2, 3, 4, 5], 1, 3).
b(P) :- a(X, Y, Z), P = substring(X, Y, Z).
@output("b").
Expected results
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.

Example
a([0, 1, 2, 3, 4, 5]).
b(3).
b(2).
c(Y, J) :- a(X), b(Y), J = contains(X, Y).
@output("c").
Expected results
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.

Example
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").
Expected results
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.

Example
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").
Expected results
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.

Example
a(["a", "b"], [1, 2]).
a(["a", "b"], "c").
c(J) :- a(X, Y), J = concat(X, Y).
@output("c").
Expected results
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.

Example
a(["a", "b"], [1, 2]).
a(["a", "b"], "c").
c(J) :- a(X, Y), J = X + Y.
@output("c").
Expected results
c(["a", "b", 1, 2])
c(["a", "b", "c"])

string_length

The operation string_length(List) returns the length of List.

string_length(List)

Where:

  • List is the list whose length is to be computed.

Example
a([0, 1, 2, 3, 4, 5]).
b(J) :- a(X), J = string_length(X).
@output("b").
Expected results
b(6)