Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a predicate called count(X, L, N) that is true if and only if item X occurs exactly N times in list L. For example:

image text in transcribed
image text in transcribed
Write a predicate called count(X, L, N) that is true if and only if item X occurs exactly N times in list L. For example: count(spam, [oh, spam, spam, we, love, spam), N). % user query N-3; % prolog replies. We type ; to ask if there's any more false % nope, no more! count(X, Coh, spam, spam, we, love, spam), N). % user query X = oh, N = 1; % any more? X-spam, N = 3; % any more? X = we, = 1; % any more? x = love, N = 1; % any more? N = 0. There's a good chance that your code will report more options, including that "spam" occurs 3 times but also twice and also once. That's an error and it's likely due to the order in which things appear. This is a good opportunity to think carefully about why order matters. By the way, the "N=0" is fine, but it's OK if your code also doesn't produce this. % some "nice" prolog settings... :- set_prolog_flag( prompt_alternatives_on, groundness ). : - set prolog_flag(toplevel_print_options, [quoted(true), portray(true), attributes (portray), max_depth(999), priority (699)]). : - set_prolog_flag(answer_write_options, [quoted(true), portray(true), attributes (portray), max_depth(999), priority (699)]). % We start you with some starter code, which handles the case of an empty list. %%%%%%%%% % STARTER % %%%%%%%%%%% count(_, 0, 0). The empty list contains no occurrences of X Finish writing the code for the other two cases. % YOU DO % count(X, [X Rest), N) :- fail. count(X, Y| Rest), N) :- fail. Write a predicate called count(X, L, N) that is true if and only if item X occurs exactly N times in list L. For example: count(spam, [oh, spam, spam, we, love, spam), N). % user query N-3; % prolog replies. We type ; to ask if there's any more false % nope, no more! count(X, Coh, spam, spam, we, love, spam), N). % user query X = oh, N = 1; % any more? X-spam, N = 3; % any more? X = we, = 1; % any more? x = love, N = 1; % any more? N = 0. There's a good chance that your code will report more options, including that "spam" occurs 3 times but also twice and also once. That's an error and it's likely due to the order in which things appear. This is a good opportunity to think carefully about why order matters. By the way, the "N=0" is fine, but it's OK if your code also doesn't produce this. % some "nice" prolog settings... :- set_prolog_flag( prompt_alternatives_on, groundness ). : - set prolog_flag(toplevel_print_options, [quoted(true), portray(true), attributes (portray), max_depth(999), priority (699)]). : - set_prolog_flag(answer_write_options, [quoted(true), portray(true), attributes (portray), max_depth(999), priority (699)]). % We start you with some starter code, which handles the case of an empty list. %%%%%%%%% % STARTER % %%%%%%%%%%% count(_, 0, 0). The empty list contains no occurrences of X Finish writing the code for the other two cases. % YOU DO % count(X, [X Rest), N) :- fail. count(X, Y| Rest), N) :- fail

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Office 365 For Beginners 2022 8 In 1

Authors: James Holler

1st Edition

B0B2WRC1RX, 979-8833565759

More Books

Students also viewed these Databases questions