简体   繁体   中英

lists intersection in prolog

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:)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM