prolog

Times, Quotient and Remainder predicates in Prolog

会有一股神秘感。 提交于 2020-07-09 11:56:31
问题 how can I do the following. I needed to define the predicate shownumber (X,N) , which is true when the symbol X corresponds to the natural number N. For example, shownumber(s(zero),1) is true. Okay, now I've got a predicate: shownumber (zero, 0). shownumber (s (N), X): - shownumber (N, Y), X is Y + 1. Now I need to use the shownumber (X, Y) predicate to define: 1) times (X, Y, Z) which is true if X * Y = Z. 2) quotient (X, Y, Q) which is true if X / Y = Q (in natural number arithmetic) 3)

Splitting a list before and after a particular element in prolog (without using “split” predicate?)

我怕爱的太早我们不能终老 提交于 2020-07-09 05:53:37
问题 I'm trying to split a list into the items before a specific element (specifically the word "stop") as well as the items after this element. I know you can use split to do this, but I'm new to prolog and so I'm trying to manipulate things without using these functions currently, and so I'd really like to know if this is possible? (and maybe some pointers in the right direction) i.e. with the list; L = [tea,coffee,sugar,cake,stop,meat,fish,eggs,flour] I'd ideally want to split the list at 'stop

How to obtain the cost of each employee in Prolog?

北战南征 提交于 2020-06-29 05:04:37
问题 How would I be able to obtain the totalcost and loop through each employee to get the cost individually? Expected output: ?- make_team([batman, superman, aquaman], Heroes, TotalCost). Heroes = [[batman, bruce, wayne, 342000], [superman, clark, kent, 475000], [aquaman, arthur, curry, 5000]], TotalCost = 822000. % Returns a list of available heroes. % Each hero's information is also stored as a list. employees(E) :- E = [ [superman, clark, kent, 475000], [batman, bruce, wayne, 342000], [wonder

Don't repeat solutions in Prolog

我只是一个虾纸丫 提交于 2020-06-27 07:36:19
问题 Suppose you have a database with the following content: son(a, d). son(b, d). son(a, c). son(b, c). So a and b are sons of d and c. Now you want to know, given a bigger database, who is brother to who. A solution would be: brother(X, Y) :- son(X, P), son(Y, P), X \= Y. The problem with this is that if you ask "brother(X, Y)." and start pressing ";" you'll get redundant results like: X = a, Y = b; X = b, Y = a; X = a, Y = b; X = b, Y = a; I can understand why I get these results but I am

convert float to integer in prolog

走远了吗. 提交于 2020-06-25 00:58:27
问题 How to convert float to integer in prolog? I tried: ?- integer(truncate(sqrt(9))). false. ?- integer(round(sqrt(9))). false. 回答1: The predicate integer/1 that you used is true iff its argument is an integer. Since the term truncate(sqrt(9)) is not an integer, the predicate does not hold and therefore fails for this term. There are at least two ways to get what you want: Solution 1: Quick and broken You can use the predicate (is)/2 for conversion between different number representations. In

Nested Predicates In Prolog

送分小仙女□ 提交于 2020-06-16 17:24:34
问题 I am trying to write a predicate that ‘exists’ inside the ‘scope’ of another predicate . The reason I need this is because both predicates make use of the same very large parameters/arrays and the predicate I want to nest is doing self recurssion many times , so I want to avoid copying the same parameters . So , is there any way i can do this in Swi-Prolg ? Thanks in advance . 回答1: You don't need to. You have to realize that all the terms "named" by Prolog variable names are already global ,

How to negate in Prolog

我们两清 提交于 2020-06-11 18:21:30
问题 I'm new to PROLOG and am at the very beginning of the exercises on this page. Given the rules parent(X, Y) and male(X), I'm trying to define a rule mother(X, Y) as mother(X, Y) :- not(male(X)), parent(X, Y). However, in GNU Prolog I get the following error: | ?- mother(lina, julia). uncaught exception: error(existence_error(procedure,not/1),mother/2) | ?- 回答1: \+/1 is the ISO Prolog predicate to "negate". Note that "negate" means here not provable at that point. You can refer to this

How to negate in Prolog

一世执手 提交于 2020-06-11 18:18:14
问题 I'm new to PROLOG and am at the very beginning of the exercises on this page. Given the rules parent(X, Y) and male(X), I'm trying to define a rule mother(X, Y) as mother(X, Y) :- not(male(X)), parent(X, Y). However, in GNU Prolog I get the following error: | ?- mother(lina, julia). uncaught exception: error(existence_error(procedure,not/1),mother/2) | ?- 回答1: \+/1 is the ISO Prolog predicate to "negate". Note that "negate" means here not provable at that point. You can refer to this

How to negate in Prolog

泄露秘密 提交于 2020-06-11 18:17:08
问题 I'm new to PROLOG and am at the very beginning of the exercises on this page. Given the rules parent(X, Y) and male(X), I'm trying to define a rule mother(X, Y) as mother(X, Y) :- not(male(X)), parent(X, Y). However, in GNU Prolog I get the following error: | ?- mother(lina, julia). uncaught exception: error(existence_error(procedure,not/1),mother/2) | ?- 回答1: \+/1 is the ISO Prolog predicate to "negate". Note that "negate" means here not provable at that point. You can refer to this

Prolog program returns false

∥☆過路亽.° 提交于 2020-06-09 06:26:29
问题 I implemented the following power program in Prolog: puissance(_,0,1). puissance(X,N,P) :- N>0,A is N-1, puissance(X,A,Z), P is Z*X. The code does what is supposed to do, but after the right answer it prints "false.". I don't understand why. I am using swi-prolog. 回答1: You can add a cut operator (i.e. ! ) to your solution, meaning prolog should not attempt to backtrack and find any more solutions after the first successful unification that has reached that point. (i.e. you're pruning the