prolog

Predicate that checks deadlock

半世苍凉 提交于 2020-04-17 19:29:29
问题 I just want someone to help me in this, because I want a function that can check deadlock for the below code. So, by checking the deadlock we will get the execution order of the process. If its in safe state with no deadlock and if there is a deadlock, just print out false. But I could not do it in Prolog, so can someone help me out to modify the below code to make it print false if its having a deadlock? processes([1,2,3,4]). request(1,r1). request(3,r2). allocated(1,r2). allocated(2,r1).

List processing calculation in Prolog to find a destination friends will visit

这一生的挚爱 提交于 2020-04-17 19:05:47
问题 I'm trying to write a predicate that calculates which destination a group of friends will visit. The friends list their countries of preferences like this choice(marie, [peru,greece,vietnam]). choice(jean, [greece,peru,vietnam]). choice(sasha, [vietnam,peru,greece]). choice(helena,[peru,vietnam,greece]). choice(emma, [greece,peru,vietnam]). I want to write a predicate called where that takes 2 arguments to perform the calculation. The formula I have in mind is that the first country is worth

Prolog, given N and find all numbers not divisible by 3 and 5 and these numbers must be smaller than N

耗尽温柔 提交于 2020-04-17 07:24:21
问题 I have problem with find a solution to the problem. Divisible/2 predicate examines whether a number N is divisible by one of numbers in the list divisible([H|_],N) :- N mod H =:= 0. divisible([H|T],N) :- N mod H =\= 0, divisible(T,N). I need to build a predicate find that will find Number < N that are not divisible by the list of numbers example input/output: ?- find(5, [3,5],Num). output is : Num = 4; Num = 2; Num = 1. False Here N is 5 and list of number is [3,5] Current Code: findNum(1, LN

Prolog, given N and find all numbers not divisible by 3 and 5 and these numbers must be smaller than N

你说的曾经没有我的故事 提交于 2020-04-17 07:24:06
问题 I have problem with find a solution to the problem. Divisible/2 predicate examines whether a number N is divisible by one of numbers in the list divisible([H|_],N) :- N mod H =:= 0. divisible([H|T],N) :- N mod H =\= 0, divisible(T,N). I need to build a predicate find that will find Number < N that are not divisible by the list of numbers example input/output: ?- find(5, [3,5],Num). output is : Num = 4; Num = 2; Num = 1. False Here N is 5 and list of number is [3,5] Current Code: findNum(1, LN

Prolog, Dynamic Programming, Fibonacci series

若如初见. 提交于 2020-04-16 05:47:22
问题 I should preface this by saying this is a homework problem that I am having issues with, and Im not sure if that sort of thing is allowed around here, but I dont know where else to turn to. This is the question I've been asked: In the sample code for this question, you can see a Fibonacci predicate fibSimple/2 which calculates the Fibonacci of X, a natural number. The problem with the naive recursive solution, is that you end up recalculating the same recursive case several times. See here

prolog find minimum value query

不羁岁月 提交于 2020-04-16 05:43:21
问题 If I have the facts in following format: person(name,age). How can I write a query to find the youngest person? I have tried using recursion but I kept getting stuck in infinite loops. So far from all the reading I done I found out I need to use the ! cut operator. Any help would be very much appreciated. 回答1: You definitely do not need to use the cut operator. I'm hard-pressed to imagine what that solution would look like. The simplest thing to do is to make a query of this sort: youngest

Prolog - deleting pairs with the same first value from list

Deadly 提交于 2020-04-15 06:50:30
问题 i have list of objects like this list([obj(x,y),obj(x,z),obj(a,b),obj(b,c)]). and i would like to remove those elements that share the same first value, so i can work with the modified list. In this case the final list would look like this list([obj(a,b),obj(b,c)] Could anyone help please? I am really struggling with this one. 回答1: Solving this problem efficiently is not trivial for a beginner. Assuming that the elements of the list are ground, we can start by noticing that sorting the list

Prolog - deleting pairs with the same first value from list

僤鯓⒐⒋嵵緔 提交于 2020-04-15 06:49:08
问题 i have list of objects like this list([obj(x,y),obj(x,z),obj(a,b),obj(b,c)]). and i would like to remove those elements that share the same first value, so i can work with the modified list. In this case the final list would look like this list([obj(a,b),obj(b,c)] Could anyone help please? I am really struggling with this one. 回答1: Solving this problem efficiently is not trivial for a beginner. Assuming that the elements of the list are ground, we can start by noticing that sorting the list

Prolog - count occurrence of number

巧了我就是萌 提交于 2020-04-13 08:06:11
问题 I want to write predicate which can count all encountered number: count(1, [1,0,0,1,0], X). X = 2. I tried to write it like: count(_, [], 0). count(Num, [H|T], X) :- count(Num, T, X1), Num = H, X is X1 + 1. Why doesn't work it? 回答1: Why doesn't work it? Prolog is a programming language that often can answer such question directly. Look how I tried out your definition starting with your failing query: ?- count(1, [1,0,0,1,0], X). false. ?- count(1, Xs, X). Xs = [], X = 0 ; Xs = [1], X = 1 ; Xs

Prolog - count occurrence of number

蹲街弑〆低调 提交于 2020-04-13 08:05:07
问题 I want to write predicate which can count all encountered number: count(1, [1,0,0,1,0], X). X = 2. I tried to write it like: count(_, [], 0). count(Num, [H|T], X) :- count(Num, T, X1), Num = H, X is X1 + 1. Why doesn't work it? 回答1: Why doesn't work it? Prolog is a programming language that often can answer such question directly. Look how I tried out your definition starting with your failing query: ?- count(1, [1,0,0,1,0], X). false. ?- count(1, Xs, X). Xs = [], X = 0 ; Xs = [1], X = 1 ; Xs