0

Hello I'm doing a project on prolog and still grasping with the language. I currently have a list of lists, in which the sublists have coordinates.

My aim is to select the sublist with a certain coordinate X and then to cut that list from it's first element up to X. (only one list)

I thought that at first the problem would to be to select the sublist with member. So I tried to do so.

sublist([],_, []).
sublist([H|R],Pos, [H|Aux]) :-
    member(Pos,H), !,
    sublist(R,Pos, Aux).
sublist([H|R],Pos, Aux) :-
    sublist(R,Pos,Aux).

This doesn't work:

?- sublist([[(1,2),(1,3),(3,5)],[(1,5),(1,2)]], (1,5), X).
X = [].

What I wanted for this first stage was something like:

?- sublist([[(1,2),(1,3),(3,5)],[(1,5),(1,2)]], (1,5), X).
X = [(1,5),(1,2)].

(afterwards, when I have the sublist selected I planned on doing a similar thing in which I add elements to an empty list until the element equals X).

So that the end result would be:

?- sublist([[(1,2),(1,3),(3,5)],[(1,5),(1,2)]], (1,5), X).
X = [(1,5)].

What am I doing wrong? I'm still learning to think in Prolog so I apologize for obvious logic mistakes.

false
  • 10,264
  • 13
  • 101
  • 209
DTek
  • 371
  • 1
  • 5
  • 13
  • 2
    Related (i.e., same homework question): https://stackoverflow.com/questions/50155722/print-a-list-inside-a-nested-list-that-contains-an-element – Isabelle Newbie May 05 '18 at 11:34
  • Also, my Prolog spits out warnings, including: `Clauses of sublist/3 are not together in the source-file ... Current predicate: sulist/3`. You have a typo you need to fix. – Isabelle Newbie May 05 '18 at 11:37
  • Fixed! Though the typo isn't the source of the problem (I translated from my code and missed it). Checking the link you sent, thanks! – DTek May 05 '18 at 11:40
  • The link sent gave me the general idea to make this work, thanks and sorry for the duplicate! – DTek May 06 '18 at 01:01

1 Answers1

0

If I understand your problem correctly:

sublist(L, Elem, M) :-
    member(M, L),
    member(Elem, M).

(L contains a list M, which contains and element Elem)

gives that result:

?- sublist([[(1,2),(1,3),(3,5)],[(1,5),(1,2)]], (1,5), X).
X = [(1, 5),  (1, 2)] ;
false.
fferri
  • 18,285
  • 5
  • 46
  • 95