prolog

Generate list - geometric progression

不羁岁月 提交于 2019-12-22 10:49:15
问题 I'd like to generate a geometric progression list using a predicate with 4 parameters - the list where the progression will be generated, the length of this list, the start element, and the multiplier of the progression. What I've done so far is having only a 3-parameter predicate to generate the geometric progression without stopping : gengeom([X],X,_). gengeom([H|Tail],H,Q):-X is H*Q,gengeom(Tail,X,Q). And this query gives me all progressions with start element 1 and multiplier 2 : ?

what's the difference between #= and =:= in SWI prolog

馋奶兔 提交于 2019-12-22 10:44:23
问题 What is the difference between #= and =:= in SWI prolog. I have found the definition from SWI prolog, but still confused about it. http://www.swi-prolog.org/pldoc/man?section=arithpreds http://www.swi-prolog.org/pldoc/man?section=clpfd-arith-constraints ?- 3=:=3. true. ?- (3-2) =:= (9-8). true. ?- 3 #= 3. true. ?- (3-2) #= (9-8). true. 回答1: What's the difference between #= and =:= in SWI prolog ? The difference is that #=/2 is a CLPFD library operator (you need to execute: use_module(library

Printing path in Prolog

强颜欢笑 提交于 2019-12-22 10:29:04
问题 I want to print the path of nodes in a directed graph. This code works properly for an edge but didn't work for the whole path. It returns false when it comes to path. Here is my code but it is only running for just an edge and not for the whole path. Kindly help me out. Here is my code: path(Node1, Node2, X) :- edge(Node1, Node2), append([Node1], [Node2], X). path(Node1, Node2, X, N) :- edge(Node1, SomeNode), append([Node1], [SomeNode], X), path(SomeNode, Node2, X, N), append([], [Node2], X)

How Prolog is used and implement the real-world application [closed]

点点圈 提交于 2019-12-22 10:12:55
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I am curious about this. I must learn Prolog for my course, but the applications that I seen mostly are written using C++, C# or Java.

Parse Variables Using DCG

廉价感情. 提交于 2019-12-22 09:30:25
问题 I am having trouble parsing sequences that begin with capital letters into variables using Prolog's DCG notation. For instance, if I have the string f a X y Z X and a DCG that parses this string, is there any way to parse each capitalized letter into a unique Prolog variable. E.g., parse Y to a variable and each X to a variable? The intended application would be to build the functor T = f(a,X,y,Z,X) via a DCG rule ending with the statement {T =.. [Head|Args]} 回答1: Maybe you are looking for

how can i draw star triangle using recursive in prolog?

大城市里の小女人 提交于 2019-12-22 08:59:25
问题 this code using to draw triangle please can any one explain how it work predicates star(integer). count(integer,integer). clauses star(1):-write('*'),!. star(X):-X<=0,!. star(X):-count(1,X),Z=x-1,nl,star(Z),!. count(X,Y):-X<=Y,write('*'),X1=X+1,count(X1,Y),!. count(X<Y):-X>Y,!. this code draw 5 star ,4,3,2,1 how i doing to begin from 1,2,3,4,5 回答1: You must pass around the upper limit: star :- star(0, 5). star(C, X) :- C < X, count(0, C), C1 is C+1, star(C1, X). star(C, X) :- C >= X. count(X,

Understanding rules - false as answer

☆樱花仙子☆ 提交于 2019-12-22 08:29:47
问题 I am new in Prolog and I was just thinking that why this rule giving me false result after one true. likes(1,banana). likes(1,mango). test :- likes(1,banana),likes(1,mango). ?- test. true; false. I want to know the reason behind this false. 回答1: The way prolog works is by evaluating queries until a fail by negation. Here, you've established two facts: likes(1, banana). which says "1 likes banana" likes(1, mango). which says "1 likes mango" Then you have established a rule, which basically

Prolog Path Finding

余生长醉 提交于 2019-12-22 08:11:37
问题 If I have the following predicate door , which declare that there is a door between the two rooms: door(office, hall). door(kitchen, office). door(hall, "dining room"). door(kitchen, cellar). door("dining room", kitchen). And the predicate doorstate which declares the state of a door: doorstate(hall, office, closed). doorstate(hall, "dining room", opened). doorstate("dining room", kitchen, opened). doorstate(kitchen, office, opened). doorstate(kitchen, cellar, opened). There is a pathway

Implementing user-defined arithmetic functions

偶尔善良 提交于 2019-12-22 07:09:07
问题 How can I add a function (e.g., hammingweight) and use it in expressions occuring in the right-hand side is some (is)/2 goal? Could something like goal_expansion or term_expansion help here? I acknowledge that this is not a big feature, but it could increase readability of some of my Prolog programs. Writing a custom (is)/2 predicate (implementing a custom expression evaluator) is do-able, but I would like to keep runtime overhead low, as I don't want to sacrifice readability for runtime

I want to make this prolog file a stand-alone EXE file

不羁岁月 提交于 2019-12-22 06:45:17
问题 I am new to prolog and I have written this code but I want to make this prolog file a stand-alone EXE file and I dont know how to go about it. I am using swi-prolog to consult the file. I need step by step tutorial on making this program executable using swi-prolog.I saved it as 'computer.pl' here is the code below. main :- identify. identify :- write('Welcome to Micro-Computer troubleshooting'), nl, write('Please do not forget to type all your answers with a period(.) in the end'), nl,