0

How can i "smoothen out" a list?

I want this to be true: smooth([1,[2,3,4],[5,[6,7]],8], [1,2,3,4,5,6,7,8]).

This is what i tried so far but it won't work

smooth([], _).
smooth([H|T], [H|L2]) :-
  \+ isList(H),
  smooth(T, L2).
smooth([H|T], L2) :-
  isList(H),
  smooth(H, L2),
  smooth(T, L2).

My isList works correctly.

Any suggestions?

false
  • 10,264
  • 13
  • 101
  • 209
DragonS
  • 31
  • 1
  • 7
  • The name for this predicate is usually `flatten/2` – false Feb 13 '20 at 10:18
  • Ok and how do i write it? I bet on my exam they will be really impressed if i write flatten/2 instead of a whole code. – DragonS Feb 13 '20 at 10:20
  • See: flatten/2 [source code](https://www.swi-prolog.org/pldoc/doc/_SWI_/library/lists.pl?show=src#flatten/2) – Guy Coder Feb 13 '20 at 10:22
  • Now that actually helped. – DragonS Feb 13 '20 at 10:23
  • @DragonS: I marked your question as duplicate, giving a reference with a correct solution. – false Feb 13 '20 at 10:26
  • 1
    @GuyCoder: The referenced definition fails for `flatten(X,[])`, yet succeeds for `X = [], flatten(X, [])`. – false Feb 13 '20 at 10:35
  • @false "Those who don't know history are doomed to repeat it." Thanks. I know you know many of these type of issues with Prolog code, are they collected and documented somewhere? I know of [Conformity Testing I: Syntax](https://www.complang.tuwien.ac.at/ulrich/iso-prolog/conformity_testing). I don't see `flatten/2` – Guy Coder Feb 13 '20 at 10:55
  • @GuyCoder: See my comments here on SO. [SEDE](https://data.stackexchange.com/tutorial) will help you. – false Feb 13 '20 at 11:00
  • @false Thanks, but for now I don't have the time to dedicate to using SEDE, but will keep it in the back of the mind as a future data mining project. – Guy Coder Feb 13 '20 at 11:04
  • `Now that actually helped.` Just to let you know. I am at like level 4 with Prolog and @false is at level 10, so if you see anything of his, disregard what most others say on the [tag:prolog] tag. There are a few exceptions to that, but you will have to learn who they are and those are typically equal to @false. – Guy Coder Feb 13 '20 at 11:08
  • @GuyCoder: W.r.t. SEDE: [shortcut](https://data.stackexchange.com/stackoverflow/query/1193796/user-comments-with-score) – false Feb 13 '20 at 11:44
  • @false Thanks. I don't know what the SEDE query wants for `UserId` so I will come back to this latter. – Guy Coder Feb 13 '20 at 12:05
  • Click on my name to get my `UserId`. I.e. 772868 – false Feb 13 '20 at 12:08
  • Hovering suffices... – false Feb 13 '20 at 13:41

0 Answers0