[ library(lists_of_structures) | Reference Manual | Alphabetic Index ]

same_key_prefix(+Key, +All, -KeyVal, -Prefix, -Rest)

Get the maximum prefix of a list with identical key values
Key
Key argument position (non-negative integer, or list of those)
All
List of structures
KeyVal
Variable, or common key
Prefix
Variable, or list of structures
Rest
Variable, or list of structures

Description

The list All is split into two sublists Prefix and Rest, such that Prefix contains all the leading elements of All whose Key arguments are identical (in the sense of ==/2), and Rest contains the remainder of the list. Concatenating the Prefix and Rest will yield the original list All. The key value of the Prefix is returned as KeyVal.

Modes and Determinism

Fail Conditions

List All is empty

Exceptions

(4) instantiation fault
Arguments are insufficiently instantiated
(5) type error
Some argument or its components are of the wrong type
(6) out of range
Some structure does not have a Key'th argument

Examples

    ?- same_key_prefix(1, [f(a,1),f(a,2),f(c,3),f(b,4),f(a,5)],
                       KeyVal, Prefix, Rest).
    KeyVal = a
    Prefix = [f(a,1), f(a,2)]
    Rest = [f(c,3), f(b,4), f(a,5)]
    Yes (0.00s cpu)

    ?- same_key_prefix(1, [], KeyVal, Prefix, Rest).
    No (0.00s cpu)

See Also

group_by_key / 3, group_with_key / 3, separate_by_key / 5