0

using append/2 we can aggregate a list of lists to one list

append([[a], [b], [c]], L) .
L= [a, b, c].

at this point, the question that arises is; how to do it with a list as follows :

L=[[[[a]]],[[[[c]]]],[[c]]]
false
  • 10,182
  • 12
  • 93
  • 182
Ans Piter
  • 583
  • 1
  • 5
  • 16
  • 3
    see [flatten](http://www.swi-prolog.org/search?for=flatten%2F2)/2 – CapelliC Mar 15 '16 at 14:34
  • 1
    You can use `flatten/2` as @CapelliC points out, but where is the list coming from? If you have a predicate which is accumulating such a list and finding you need to flatten when you're done, that's an indicator that your predicate may be in need of refactoring to avoid the embedded lists. – lurker Mar 15 '16 at 14:44
  • 1
    The common `flatten/2` has a bunch of inherent anomalies. Avoid it, unless you produce a safe, logical definition. – false Mar 15 '16 at 15:49
  • @false flatten/2 not available in sisctus – Ans Piter Mar 15 '16 at 20:41
  • @lurker , yes rightly, it is a list that accumulates in order to parse an expression – Ans Piter Mar 15 '16 at 20:48
  • I find a solution in this [link](http://stackoverflow.com/questions/9059572/flatten-a-list-in-prolog) – Ans Piter Mar 15 '16 at 21:10
  • 2
    @AnsPiter: There is a [good solution](http://stackoverflow.com/a/30824413/772868) by repeat, but it is quite complex. The others are incorrect. – false Mar 15 '16 at 21:29
  • @false what about the answer of sharky! – Ans Piter Mar 15 '16 at 21:42
  • 1
    `X=[a], flatten2(X,[a]).` succeeds as one might expect, but `flatten2(X,[a]), X=[a].` fails. Even `flatten2(X,[a])` alone fails. – false Mar 16 '16 at 09:09
  • 1
    `flatten2([],[[]]).` succeeds. `[[]]` is certainly not a flattened list. – false Mar 16 '16 at 09:09

0 Answers0