prolog

Prolog Program To Check If A Number Is Prime

岁酱吖の 提交于 2019-12-29 07:07:29
问题 I wrote the following program based on the logic that a prime number is only divisible by 1 and itself. So I just go through the process of dividing it to all numbers that are greater than one and less than itself, but I seem to have a problem since I get all entered numbers as true. Here's my code... divisible(X,Y) :- Y < X, X mod Y is 0, Y1 is Y+1, divisible(X,Y1). isprime(X) :- integer(X), X > 1, \+ divisible(X,2). Thanks in advance :) 回答1: I'm a beginner in Prolog but managed to fix your

“Or” procedure in prolog

。_饼干妹妹 提交于 2019-12-29 02:08:08
问题 I'm doing a prolog program for college that is a bit like the cluedo game. I have six suspects with different traits: suspect(Name, Age, Weapon, Shape, Object, Shoes) The goal is to implement a series of clues so that the program says which the different traits of all the six suspects. For example: suspect(Hannibal Lecter,67,knife,'in good shape',mac,'high heels') I'm having problems trying to implement the clue or(suspect1, suspect2, suspect3, listOfSuspects) This clue is supposed to say

Sorting a list in Prolog

。_饼干妹妹 提交于 2019-12-28 22:11:53
问题 Prolog has a unique way of handling things, especially since practically every operation involves recursion of one sort or another. One of the classic examples every language has is sorting a list of integers into ascending order. What is an optimal way (without using too many built-in predicates, which precludes a sort/2 predicate, of course) to sort a random list of integers? 回答1: Roman Barták's Prolog Programming site gives examples of different sort algorithms, ending with an optimized

Sorting a list in Prolog

拥有回忆 提交于 2019-12-28 22:10:12
问题 Prolog has a unique way of handling things, especially since practically every operation involves recursion of one sort or another. One of the classic examples every language has is sorting a list of integers into ascending order. What is an optimal way (without using too many built-in predicates, which precludes a sort/2 predicate, of course) to sort a random list of integers? 回答1: Roman Barták's Prolog Programming site gives examples of different sort algorithms, ending with an optimized

Force Prolog to choose unique values of variables

早过忘川 提交于 2019-12-28 22:03:42
问题 OK I am new to Prolog, so excuse me if this is something trivial, but I can't seem to find a proper elegant answer to this. I am trying to work out the exercise here on learnprolognow.org, exercise 2.4 (the crossword). The exercise provides these facts: word(astante, a,s,t,a,n,t,e). word(astoria, a,s,t,o,r,i,a). word(baratto, b,a,r,a,t,t,o). word(cobalto, c,o,b,a,l,t,o). word(pistola, p,i,s,t,o,l,a). word(statale, s,t,a,t,a,l,e). And the solution I came up with to solve the crossword

Prolog - differences between red cut and green cut

ぃ、小莉子 提交于 2019-12-28 14:31:26
问题 I started learning prolog, and wanted to make the whole cuts thing clearer. I have read that "green cut doesnt change declarative meaning of the program, while red cut does". But, the meaning of the program isnt really pure declarative (just from the fact that prolog actually backtracks for all options). Here is an example: p(1). p(2) :- !. p(3). it has been said that this is green cut. But if I run this: p(X), X =:= 3. I will get "true" without a cut, and "false" with a cut. so, what do I

GNU Prolog assert error

ⅰ亾dé卋堺 提交于 2019-12-28 06:59:06
问题 I am new to Prolog, but I am stuck at this supposedly simple command. I have loaded a knowledge base with no errors, and whenever I try do assert (and even help ) I get the following message: uncaught exception: error(existence_error(procedure,assert/1),top_level/0) {2} What am I exactly missing? Appreciated. 回答1: Use assertz/1 or asserta/1 instead. GNU-Prolog does not provide assert/1 because only asserta/1 and assertz/1 are defined in the standard. Note that while asserta/1 always had one

What are the pros and cons of using manual list iteration vs recursion through fail

别来无恙 提交于 2019-12-28 06:56:46
问题 I come up against this all the time, and I'm never sure which way to attack it. Below are two methods for processing some season facts. What I'm trying to work out is whether to use method 1 or 2, and what are the pros and cons of each, especially large amounts of facts. methodone seems wasteful since the facts are available, why bother building a list of them (especially a large list). This must have memory implications too if the list is large enough ? And it doesn't take advantage of

Fill list in SWI-Prolog

一个人想着一个人 提交于 2019-12-28 04:33:10
问题 I am trying to fill a list of given length N with numbers 1,2,3,...,N. I thought this could be done this way: create_list(N,L) :- length(L,N), forall(between(1,N,X), nth1(X,L,X)). However, this does not seem to work. Can anyone say what I am doing wrong? 回答1: I don't have a prolog interpreter available right now, but wouldn't something like... isListTo(N, L) :- reverse(R, L), isListFrom(N, R). isListFrom(0, []). isListFrom(N, [H|T]) :- M is N - 1, N is H, isListFrom(M, T). reverse can be done

Get elements from list of lists

好久不见. 提交于 2019-12-28 03:10:17
问题 is it possible to get all elements from list of lists in Prolog? Something like: We have getElements([[[a,b,[c]],d,e],f,g,[h,[i,j]]],S) and the result is: S = [a,b,c,d,e,f,g,h,i,j] ... Thanks for help. 回答1: In SWI-Prolog (and maybe others), you can use flatten/2 : ?- flatten([[[a,b,[c]],d,e],f,g,[h,[i,j]]], S). S = [a, b, c, d, e, f, g, h, i|...]. Note that the SWI-Prolog manual page for flatten/2 includes the following statement: Ending up needing flatten/3 often indicates, like append/3 for