Prolog Problems: 1 – Write a Prolog program that finds the number of elements in
ID: 3572750 • Letter: P
Question
Prolog Problems:
1 – Write a Prolog program that finds the number of elements in a given list.
2 – Write a Prolog program that finds whether the elements in a given list are sorted in ascending order.
For example, when giving Prolog the following queries you should get these responses:
ascending([1,2,4,6,8,11]) à Prolog replies true
ascending([11,22,43,16,88,91]) à Prolog replies false
ascending([0,123,345]) à Prolog replies true
3 – You are given two jugs, a 5-gallon one and a 3-gallon one. Neither of them has any measuring markers on it. There is a tap that can be used to fill the jugs with water. The goal is to obtain exactly 4 gallons of water. Write a Prolog program that solves this problem by showing all the steps required for any given solution, and all possible solutions.
To solve this problem you should use the same approach that we used in class to solve the missionaries and cannibals riddle using Prolog. Define a predicate to describe any state, the goal state, all possible moves to transition from one state to the other, rules to determine valid states, a predicate that defines how to change state, and a recursive rule used to traverse the possible paths and find the goal. Also make sure to avoid cycling if cycles can happen.
4 – Write a Prolog program to solve the following age riddle: “What day is today if the day before yesterday I was thirteen years old and next year I will be sixteen years old?”
Hint: The answer to the riddle is a specific day and month, the year is flexible, meaning that as long as the day and month are correct the riddle will work irrespective of the year. Your prolog predicate that finds the answer should be written similar to this: answer(D,M) where D is the day and M is the month.
Alternatively, you could also write a predicate to query in this way: answer(D,M,BD,BM) where D and M represent the present day and month (the riddle’s answer) and BD and BM are the day and month that the person in the riddle was born in. Asking the query in this way will give a more meaningful answer to the riddle because the values for BD and BM will help you understand why the values for D and M are a valid answer to the riddle.
Explanation / Answer
Hii there,
check out the solutions for various subparts:
1)prolog program to find the number of elements in the given list
2) whether the elements in a given list are sorted in ascending order.
---------------
Thanks, i hope this may help you out.....
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.