I have two questions. First, is Prolog widely used in the field of computer programming?
The second question is programming related. I'm a student working to become a programmer. My professor gave the class a Prolog assignment that baffles me. The assignment asks me to write a remove goal with 3 parameters. The first two parameters are lists, and the third is where to output the results. This goal is suppose to delete all occurrences of items in list two from list one.
Sample input and output would be:
remove([1,2,3,1], [1, 3], X).
Should produce:
X = [2]
I have been working on this for a few days and the progress I have made is the following:
% special condition for when list 2 is empty
remove(L,[],L).
% if the first item is the same on both lists delete it
remove([H|T],[H|S],Z) :- remove(T,S,Z).
% if the first item isn't the same put the first item from list one into Z
remove([R|T],[Q|S],[R|Z]) :- remove(T,[Q|S],Z).
In the case of the example input and output above this gives me the output of:
X = [2, 3, 1]
I understand that my program doesn't work because my third line moves the first item from the first list into Z if it isn't the same as the first item in the second list, and because of that it never gets evaluated. I just can't figure out a way to create a remove that would look at each element in the first list, compare it to all the elements in the second list, and then insert it into the third list if it wasn't contained in the second list.
The second question is programming related. I'm a student working to become a programmer. My professor gave the class a Prolog assignment that baffles me. The assignment asks me to write a remove goal with 3 parameters. The first two parameters are lists, and the third is where to output the results. This goal is suppose to delete all occurrences of items in list two from list one.
Sample input and output would be:
remove([1,2,3,1], [1, 3], X).
Should produce:
X = [2]
I have been working on this for a few days and the progress I have made is the following:
% special condition for when list 2 is empty
remove(L,[],L).
% if the first item is the same on both lists delete it
remove([H|T],[H|S],Z) :- remove(T,S,Z).
% if the first item isn't the same put the first item from list one into Z
remove([R|T],[Q|S],[R|Z]) :- remove(T,[Q|S],Z).
In the case of the example input and output above this gives me the output of:
X = [2, 3, 1]
I understand that my program doesn't work because my third line moves the first item from the first list into Z if it isn't the same as the first item in the second list, and because of that it never gets evaluated. I just can't figure out a way to create a remove that would look at each element in the first list, compare it to all the elements in the second list, and then insert it into the third list if it wasn't contained in the second list.