Return a list from items matching in two lists Prolog -


i new prolog , have :

compare_list(hours1, hours2, matching)  

i want return matching hours between lists hours1 , hours2 list matching

i can matches not construct list of matches.

hours1 may like: [1,2,3], hours2 may like: [2,3], this: matching hours should be: [2,3]

help appreciated.

i have implemented vennik has suggested , near want. results hours1 : [2,3,5], hours2 : [2,5] give following:

matching = [2, 5] ; matching = [2] ; matching = [5] ; matching = []  

is possible have first set without producing other 3 results?

try this:

compare_list([], _, []). compare_list([hour | hours1], hours2, [hour | matching]) :-     member(hour, hours2),     compare_list(hours1, hours2, matching). compare_list([_ | hours1], hours2, matching) :-     compare_list(hours1, hours2, matching). 

calling compare_list([1,2,3], [1,2], x), !. result in x = [1,2].


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -