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

# flatten(++MaxDepth, +NestedList, ?FlatList)

Depth-limited list flattening

++MaxDepth
  Maximum depth to flatten.
+NestedList
  List.
?FlatList
  List or variable.

## Description

Like flatten/2, but does not flatten beyond the specified depth MaxDepth. So flatten(0, List, Flat) just unifies Flat and List (no flattening), flatten(1, List, Flat) just flattens the top-level list of List, etc.

This predicate does not perform any type testing functions.

Since ECLiPSe 7.0, arrays are treated like lists, and also flattened.

### Modes and Determinism

- flatten(++, +, -) is det

### Fail Conditions

Fails if FlatList does not unify with the flattened version of NestedList.

### Resatisfiable

No.

## Examples

       Success:
          [eclipse]: flatten(0, [[1,2,[3,4],5],6,[7]], L).
          L = [[1, 2, [3, 4], 5], 6, [7]]
          yes.
          [eclipse]: flatten(1, [[1,2,[3,4],5],6,[7]], L).
          L = [1, 2, [3, 4], 5, 6, 7]
          yes.
          [eclipse]: flatten(2, [[1,2,[3,4],5],6,[7]], L).
          L = [1, 2, 3, 4, 5, 6, 7]
          yes.
          [eclipse]: flatten(3, [[1,2,[3,4],5],6,[7]], L).
          L = [1, 2, 3, 4, 5, 6, 7]
          yes.

          [eclipse]: flatten(1, a, L).
          L = [a]
          yes.

       Fail:
          [eclipse]: flatten(2, [1,[3],2], [1,2,3]).
          no.

## See Also

flatten / 2, sort / 2, sort / 4, length / 2, member / 2