Question

Write a Prolog program called replace that replaces all occurrences of one item in a list...

Write a Prolog program called replace that replaces all occurrences of one item in a list with a new item. Example under:

?- replace([apple,butter,apple,candy,apple,potato],apple,milk,Result).

Result = [milk,butter,milk,candy,milk,potato]

Homework Answers

Answer #1

PREDICATE IN PROLOG:

replace([], _, _, []). %base case for empty list
%recursive cases
%when first element of list is to be replaced, we replace it and recurse for rest of list
replace([X1|Y1], X1, X2, [X2|Y2]):- replace(Y1, X1, X2, Y2), !.
%when first element of list is not to be replaced, we keep it and recurse for rest of list
replace([X|Y1], X1, X2, [X|Y2]):- replace(Y1, X1, X2, Y2).

OUTPUT:

Regards!

Know the answer?
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
Similar Questions
Write a program in prolog to delete all reference of a particular item from a list....
Write a program in prolog to delete all reference of a particular item from a list. It should have three arguments. The list you wish to use, the item to delete, and the resulting list. Here are some example of it behaviour ?- delete_all([a,b,a,c,a,d],a,Result). Result = [b,c,d] ? - delete_all([a,b,a,c,a,d],b,Result). Result = [a,a,c,a,d]
Using Prolog: Write a program that reads an integer x and a list of integers L;...
Using Prolog: Write a program that reads an integer x and a list of integers L; then locate the list of all positions of x into L, and return the resulting list. For example, for x=2 and L=[1,2,3,4,2,5,2,6] the program should return the list R=[2,5,7]. Note:The build-in function are not allowed.
Program Instructions Write a program called censor.py. This program will have the following functionality and requirements:...
Program Instructions Write a program called censor.py. This program will have the following functionality and requirements: Has a main function: That asks the user to input a sentence Then asks the user to enter a list of words they want to replace (separated by spaces) Calls a function called replaceMultiWords passing those two parameters, which returns a filtered string Prints out the filtered (censored) string to the console Has a replaceMultiWords function that: Takes a string "sentence" and a string...
Write a python program to list all files in the given directory along with their length...
Write a python program to list all files in the given directory along with their length and last modification time. The output should contain one line for each file containing filename, length and modification date separated by tabs. https://www.tutorialspoint.com/python/python_modules.htm
In coral, write a program that first gets a list of five integers from input. Then,...
In coral, write a program that first gets a list of five integers from input. Then, get another value from the input, and output all integers less than or equal to that value. Example is the input is 50 60 140 200 75 100, the output is 50 60 75. For coding simplicity, follow every output value by space, including the last one. Then, output a new line. Such functionality is common on sites like Amazon, where users can filter...
Write a class called Item that has a string field called name and a double field...
Write a class called Item that has a string field called name and a double field called price and an integer field called quantity. In main, create a bag of type Item and place several item objects in the bag. Then in main calculate the total price of items purchased. You may remove each item and add the price to a total variable in a loop. The loop should end when the bag is empty. Using bag algorithms public final...
Write a python program that will perform text analysis on an input text using all of...
Write a python program that will perform text analysis on an input text using all of the following steps: 1. Take the name of an input file as a command line argument. For example, your program might be called using python3 freq.py example1 to direct you to process a plain text file called example. More information is given on how to do this below. 2. Read the contents of the file into your program and divide the text into a...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that inserts 20 random integers from 0 to 100 in order in a linked list object. The program will create another linked list, but with 15 random integers from 0 – 100 in order. The program then will merge those two ordered linked list into a single ordered list. The function merge should receive references to each of the list objects to be merged and...
Project File Processing. Write a program that will read in from input file one line at...
Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces,...
Write a program that will read the information from a file into a list and then...
Write a program that will read the information from a file into a list and then display the list to the screen. Remove the fifth item in the list and display the list again. Ask the program user for an entry into the list and add it to the list. Display the list one last time. disneyin.txt file daisy   123 donald   345 goofy   654 mickey   593 minnie   489 daffy   432 pluto   765 huey   321 dewey   987 lewey   554 porky   333...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT