0

i'am new in Prolog , i tried to write 5 lists and get the intersection between them , how i can achieve that, ** lists will be defined in file so it's not input from the user .

i see many resources they implement it with two lists and its work fine if i make list as query from user ... but when i try to pre-defined lists in file it's not work.


simple description of part of my project to more clarify... menus will display and user will select one from each of the season , weather condition , occasion... lists will be about what clothes are appropriate

so for example user select "winter" season, "rainy" weather condition and "wedding"occasion lists for each of them

    rainy([take_umbrella, jacket,coat]).
winter([jacket,sweater,coat,take_umbrella]).
wedding ([take_umbrella,dress,jacket,coat]).

so the result form intersection will be take_umbrella ,jacket,coat

i hope my idea is clear, and thank you in advance:)

mSU
  • 25
  • 1
  • 1
  • 3
  • 4
    If you understand how to do it with two list, then start with two of the list and that new list is intersected with the third, that output with the fourth, and that output with the fifth. – Guy Coder Nov 20 '18 at 19:14
  • 3
    The intersection of N lists is the intersection of 1 of the lists with the intersection of the remaining N-1 lists. So recursively you can easily do this using a 2-list intersection. – lurker Nov 20 '18 at 19:27
  • 3
    *when i try to pre-defined lists in file it's not work.* Perhaps you could show specifically what you've tried and what went wrong. – lurker Nov 20 '18 at 19:36
  • 1
    In addition to the other comments: a web search for "prolog list intersection" produced the [SWI-Prolog documentation on intersection/3](http://www.swi-prolog.org/pldoc/man?predicate=intersection/3) as first hit and [this question with several answers](https://stackoverflow.com/questions/9615002/intersection-and-union-of-2-lists) as second hit for me. You might find it helpful to try to build an intersection predicate for an arbitrary number of lists based on an already working predicate for the intersection of two lists ;-) – tas Nov 20 '18 at 20:34

1 Answers1

0

I try to predefined lists in file and it's work. Y your lists in file it's not work? I do not know. I fix your errors because your paste is not error free then it's work.

?- winter(Winter),
   rainy(Rainy),
   wedding(Wedding),
   intersection(Winter, Rainy, Winter_and_Rainy),
   intersection(Winter_and_Rainy, Wedding, Winter_and_Rainy_and_Wedding).
Winter = [jacket, sweater, coat, take_umbrella],
Rainy = [take_umbrella, jacket, coat],
Wedding = [take_umbrella, dress, jacket, coat],
Winter_and_Rainy = Winter_and_Rainy_and_Wedding, Winter_and_Rainy_and_Wedding = [jacket, coat, take_umbrella].

But if it's not know how many list maybe you make list and reduce.

?- % make some lists L1, L2, ..., Ln,
   foldl(intersection, [L1, L2, ..., Ln-1], Ln, Intersection).

When is winter on a rainy wedding you reduce like:

?- winter(Winter), rainy(Rainy), wedding(Wedding),
   foldl(intersection, [Winter, Rainy], Wedding, Intersection).
Winter = [jacket, sweater, coat, take_umbrella],
Rainy = Intersection, Intersection = [take_umbrella, jacket, coat],
Wedding = [take_umbrella, dress, jacket, coat].

You see order of element is change but is it problem? For me no problem.