1

The solution I came up with checks the left and right side, to make sure the left side is flattened and the right side for all possible partitions.

The equation for partitions has some errors for when I check a double nested element.

unflatten([],[]) :- !.
unflatten(flatList, [l1|lst]) :- !, unflatten(A, l1), unflatten(B, lst), append(A,B, flatList).
unflatten([l1|flatList], [l1]|lst]) :- unflatten(flatlist,lst).
unflatten([l1,l2|flatList], [[l1,l2]|lst]) :- !, unflatten(flatList,lst).
unflatten( [flatList],flatList):- !.
  • 1
    I'm starting to think that this is an academic exercise you guys must do :) It's the second new person today on stackoverflow asking the same thing. If that's true, it seems very unethical from you guys... See http://stackoverflow.com/questions/40709840/how-to-create-the-flatten-equation-in-prolog/40710166#40710166 – Hugo Sereno Ferreira Nov 21 '16 at 02:48

1 Answers1

0

An implementation of unflatten (as the reverse of a flatten) seems like a strange thing to do, because there's an infinite number of possible solutions. For example, unflatten([1, 2, 3, 4], A) would need not only to unify A with [[1], [2, 3, 4]], [[1], [2], 3, 4], [[1, 2], [3], 4] and so on, but also with monsters such as this:

 [[[[[[[[[[]]]]]]]]], [[[[[[[[[[[1]]]]]]]]]], 2, 3, 4]

Hence, either you have further restrictions on the domain of possible solutions, or I'm struggling to even conceptualize a strategy that would return useful reverses of flatten, and still be complete over such infinite domain. I'm willing to be proven wrong, though...

Hugo Sereno Ferreira
  • 8,600
  • 7
  • 46
  • 92
  • That's somewhat correct, but A is a list of lists, not a list and element or a list or empty lists. – Woof Warrior Nov 21 '16 at 04:29
  • @WoofWarrior same thing, since you can have empty lists... unless you force something like, (i) all elements are lists, (ii) no empty lists allowed. It might be possible to produce a sound solution if that's so. – Hugo Sereno Ferreira Nov 25 '16 at 00:11