Question

3. 2 marks] Write the rule remdup (List1, List2) that takes as input List1 and produces as output List2. List2 contains all i

Solve above using prolog!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer

3.

%rule remdup(List1,List2) that takes as input List1 and produces as output List2.
%List2 contains all items that appear in List1,
%except the repeated or duplicate items.

%if the List1 is empty, return the empty list. Base case.
remdup([],[]).

%if the first element of List1 is anywhere in the rest of List1, then call remdup
%with the rest of List1 as input.
remdup([H|T],C):- member(H,T),remdup(T,C).

%if the first element of List1 is not anywhere in the rest of List1, then append it to List2
%and call remdup with the rest of List1 as input.
remdup([H|T],[H|C]):- not(member(H,T)),remdup(T,C).

Sample Output

1 %rule rendup(List1,List2) that takes as input List1 and produces as output List2. 2 %List2 contains all items that appear i

4.

%find all the direct prerequisites of the course.
prereqs(C,R):- requires(C,R).
%find all the indirect prerequisites of the course.
prereqs(C,R):- requires(C,R0),prereqs(R0,R).

%find all the prerequisites of the course.
allreqs(C,L):- setof(C1,prereqs(C,C1),L).

Sample Output

8 requires (102,101) 9 requires (106,102) 10 requires (106,103) 11 requires (105,184) 12 requires (104,101) 13 requires (105,

Add a comment
Know the answer?
Add Answer to:
Solve above using prolog!! 3. 2 marks] Write the rule remdup (List1, List2) that takes as input List1 and produces as o...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT