prolog

Reaching end of list in prolog

会有一股神秘感。 提交于 2019-12-23 10:53:25
问题 I've been given the question: Define a predicate ordered/1, which checks if a list of integers is correctly in ascending order. For example, the goal ordered([1,3,7,11]) should succeed, as should the goal ordered([1,3,3,7]) , whereas the goal ordered([1,7,3,9]) should fail. So far I have this: ordered([]). ordered([N, M|Ns]):- append(M, Ns, Tail), ordered(Tail), N =< M. But it fails on every list. I have deduced that the reason it fails is because it reaches the end number in the list then

Prolog: how to convert string to integer?

微笑、不失礼 提交于 2019-12-23 10:40:05
问题 So as the title says - how do you convert a string into an integer? the idea is something like this: convert(String,Integer). examples: convert('1',1). convert('33',33). I'm using swi prolog 回答1: Use atom_number/2. E.g: atom_number('123', X). X = 123. 回答2: Assuming you really meant a string and not an atom, use number_codes . ?- number_codes(11, "11"). true. ?- number_codes(11, Str). Str = [49, 49]. % ASCII/UTF-8 ?- number_codes(N, "11"). N = 11. 回答3: Perhaps use of atom_codes(?Atom, ?String)

unique results from prolog

 ̄綄美尐妖づ 提交于 2019-12-23 10:38:46
问题 Is there a easy way to make a query in prolog only return each result once? for instance I'm trying something like: deadly(Xn) :- scary(X), Xn is X - 1, Xp is X + 1, not(safe(Xn)), safe(Xp). deadly(Xp) :- scary(X), Xn is X - 1, Xp is X + 1, not(safe(Xp)), safe(Xn). deadly(X). and getting X = 5 X = 5 X = 5 X = 5 .... Not to usefull for me. 回答1: One thing that you can do is to apply setof/3 to the predicate that generates the solutions. But note that setof/3 is implemented by applying sort/2 to

unique results from prolog

泪湿孤枕 提交于 2019-12-23 10:38:05
问题 Is there a easy way to make a query in prolog only return each result once? for instance I'm trying something like: deadly(Xn) :- scary(X), Xn is X - 1, Xp is X + 1, not(safe(Xn)), safe(Xp). deadly(Xp) :- scary(X), Xn is X - 1, Xp is X + 1, not(safe(Xp)), safe(Xn). deadly(X). and getting X = 5 X = 5 X = 5 X = 5 .... Not to usefull for me. 回答1: One thing that you can do is to apply setof/3 to the predicate that generates the solutions. But note that setof/3 is implemented by applying sort/2 to

What does the colon (:) exactly stand for in Swi-Prolog?

房东的猫 提交于 2019-12-23 09:32:31
问题 I could not find explicitly what (:) stands for in prolog. In interactive mode you can see the following evidence: ?- display(a:b). :(a,b) true. ?- display([a,b,c]). .(a,.(b,.(c,[]))) true. ?- display(a:b:c:[]). :(a,:(b,:(c,[]))) true. ?- a:b:REST = a:TAIL. TAIL = b:REST. For what purpose (:) is introduced? I could not find any details for it in www. Seems that it gives another syntactic way of talking about recursive structures as Lists. We can say that it is Right-associative, what is its

Expressing setup time with cumulatives

主宰稳场 提交于 2019-12-23 09:23:27
问题 There are many families of scheduling problems. I'm looking into a problem where I have families of jobs/tasks where the transition from one family to another family require reconfiguring the machine (setup time). I'm using cumulatives[2/3] to solve this problem, but I feel unsure how the setup time could be expressed. In this small example I have 10 tasks belonging to 3 different families. Any task can run on any machine, but a switch from one task in one family to another task in another

Two clause definition to find the maximum number on a list

冷暖自知 提交于 2019-12-23 09:19:57
问题 How would I write a two clause recursive definition to find the maximum value in a list. So far I have written this: max(L,M):- max([H|T],M):- max(T,H,M). max([],M,M). max([H|T],Y,M):- H =< Y, max(T,Y,M). max([H|T],Y,M):- H > Y, max(T,H,M). This doesn't work, it says there is a syntax error which I can't quite see, and I know it isn't two clause either. Anyone know how I could simplify it to make it two clause? 回答1: As you, I use the 'max' name for the predicate. This implementation don't

Prolog. In a query, how to put a condition on a variable that I do not want in the results?

拟墨画扇 提交于 2019-12-23 09:10:53
问题 Imagine that I have the following knowledge base which gives for each person his first name and his age. person(mary, 39). person(john, 24). person(sandy, 17). Now, I want to retrieve all the persons that are older than 20 years. Furthermore, I just want to collect their first names and not their age. Here, I want to retrieve mary and john . How to do this generally in Prolog and more specifically in SWI-Prolog? If we use a variable which is not anonymous, like: ?- person(X, Y), Y > 20.

Is an infinite list of ones sane?

戏子无情 提交于 2019-12-23 08:17:01
问题 In Prolog, is the unification X = [1|X] a sane way to get an infinite list of ones? SWI-Prolog does not have any problem with it, but GNU Prolog simply hangs. I know that in most cases I could replace the list with one(1). one(X) :- one(X). but my question is explicitly if one may use the expression X = [1|X], member(Y, X), Y = 1 in a "sane" Prolog implementation. 回答1: In Prolog, is the unification X = [1|X] a sane way to get an infinite list of ones? It depends on whether or not you consider

findall/3 creates new, unrelated variables in its resulting list

半世苍凉 提交于 2019-12-23 07:57:20
问题 ?- permutation([A,B,C],Z). Z = [A, B, C] ; Z = [A, C, B] ; Z = [B, A, C] ; Z = [B, C, A] ; Z = [C, A, B] ; Z = [C, B, A] ; false. Makes sense. I can work on a permutation of [A,B,C] and that permutation contains the same elements as in [A,B,C] , so everything I do to those elements will apply to my original list. Now: ?- findall(X, permutation([A,B,C], X), Z). Z = [[_G1577, _G1580, _G1583], [_G1565, _G1568, _G1571], [_G1553, _G1556, _G1559], [_G1541, _G1544, _G1547], [_G1529, _G1532, _G1535],