Intersection and union of 2 lists

后端 未结 7 1763
青春惊慌失措
青春惊慌失措 2020-11-27 07:41

i\'m starting up learning prolog (i use SWI-prolog) and i did a simple exercise in which i have 2 lists and i want to calculate their intersection and union. Here is my code

相关标签:
7条回答
  • 2020-11-27 08:33

    If the aim is to just 'get the job done', then swi prolog has built in primitives for exactly this purpose:

    [trace] 3 ?- intersection([1,3,5,2,4] ,[6,1,2], X).
    intersection([1,3,5,2,4] ,[6,1,2], X).
    X = [1, 2].
    
    [trace] 4 ?- union([1,3,5,2,4] ,[6,1,2], X).
    X = [3, 5, 4, 6, 1, 2].
    
    0 讨论(0)
提交回复
热议问题