Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find if a number is a member of an element in a list

Tags:

prolog

i´m trying to create a predicate that returns me the element of a list that contains a certain number given by me.

Example:

?- where_is_it( [ [1,2,3] , [1,2,7] , [4,5] , [8] ] , 7 , X ).

X=[1,2,7].

I am a relatively new prolog programmer so this is my code:

where_is_it([],_,[]). 
where_is_it([H|T],Num,H):-
    member([Num],H),!,
    where_is_it(T,Num,[]).

Thank you very much

like image 513
JayJay Avatar asked May 06 '17 13:05

JayJay


1 Answers

You could use if_/3 and memberd_t/2 from module reif in order to be more deterministic:

where_is_it([H|T], X, L) :-
  if_(memberd_t(X,H), L=H, where_is_it(T, X, L)).
like image 172
coder Avatar answered Oct 30 '22 04:10

coder