[ library(lists) | Reference Manual | Alphabetic Index ]
collection_to_list(+Collection, -List)
Convert a "collection" into a list
- Collection
- A term to be interpreted as a collection
- List
- Output list
Description
Converts various "collection" data structures into a list. Fails if it
does not know how to do this. The supported collection types are:
- List
-
The list is returned unchanged.
- Array
-
The array is converted into a list using array_list/2.
- Subscript reference Array[...]
-
subscript/3 is called to evaluate the subscript reference. If this
results in a single array element, a one-element list is returned.
If subscript/3 results in a sub-array, this is converted into a list.
For multi-dimensional sub-arrays, only the top level is converted
into a list (no implicit flattening).
- flatten(N, Collection)
-
If the collection is nested (multi-dimensional), the top N nesting
levels of the structure are converted into a flat list.
- flatten(Collection)
-
If the collection is nested (multi-dimensional), all nesting
structure is removed and a flat list is returned. All subterms that
look like list or array will be interpreted as such (including []).
Modes and Determinism
- collection_to_list(+, -) is semidet
Fail Conditions
Collection is not a collection
Examples
?- collection_to_list([a,b,[c,d]], List).
List = [a, b, [c, d]]
Yes
?- collection_to_list([](a,b,[c,d]), List).
List = [a, b, [c, d]]
Yes
?- collection_to_list([]([](a,b),[](c,d)), List).
List = [[](a, b), [](c, d)]
Yes
?- A = []([](a,b),[](c,d)),
collection_to_list(A[1..2,1], List).
List = [a, c]
Yes
?- A = []([](a,b,c),[](d,e,f)),
collection_to_list(A[1..2,2..3], List).
List = [[](b, c), [](e, f)]
Yes
?- collection_to_list(flatten([a,b,[c,d]]), List).
List = [a, b, c, d]
Yes
?- collection_to_list(flatten([](a,b,[c,d])), List).
List = [a, b, c, d]
Yes
?- A = []([](a,b,c),[](d,e,f)),
collection_to_list(flatten(A[1..2,2..3]), List).
List = [b, c, e, f]
Yes
?- L = [[a,b],[[c,d],[e,f]],g],
collection_to_list(flatten(1, L), List).
List = [a, b, [c, d], [e, f], g]
Yes
See Also
collection_to_array / 2, subscript / 3, flatten / 2, flatten / 3