prolog

Some implicit constraint

喜欢而已 提交于 2019-12-24 07:58:38
问题 When i execute this code(shown below) , it always sets implicit kind of constraint. As you can see below , it always says that D1 = D2 but there is no such explicit constraints nor any pattern matching which forces this. Or in otherwords there is some reference between D1 and D2 such that whenever D1 gets initialized, D2 gets automatically initialized. I can't see how this is happening. Can someone explain me, i tried ot figure it out with debugger but it did not help. It's a puzzle "GERALD +

Prolog, testing labeling heuristics

最后都变了- 提交于 2019-12-24 07:26:03
问题 I'm working on some experiments for comparing different labeling heuristics in Sicstus Prolog. But I keep getting into 'Resource error: insufficient memory'. I'm pretty sure I'm doing something wrong in my testcode. The following code will replicate my problem: :- use_module(library(clpfd)). :- use_module(library(lists)). atest( R, C ):- X is R * C, length( M, X), domain( M, 0, X), all_distinct( M ), statistics(walltime, [_,_SinceLast]), labeling( [],M ), statistics(walltime, [_,SinceLast]),

Multiply Two Polynomials in Prolog

我是研究僧i 提交于 2019-12-24 07:04:06
问题 I am trying to use Prolog to implement the polynomial multiplication that multiplies two polynomials. Below is the code given in SML, but I need it in Prolog. A test solution for SML is poly_mult([1.0, 5.0, 1.0], [3.0, ~10.0, 15.0]); and will return val it = [3.0,5.0,~32.0,65.0,15.0] : real list I tried to write the code in Prolog but what I have is not correct. Can anyone help? Thanks! IN SML fun poly_add (M,nil) = M | poly_add (nil,N) = N | poly_add ((m:real)::mr, n::nr) = (m+n)::poly_add

Prolog arithmetic in foreach

*爱你&永不变心* 提交于 2019-12-24 06:39:02
问题 So I'm learning Prolog. One of the things I've found to be really obnoxious is demonstrated in the following example: foreach( between(1,10,X), somePredicate(X,X+Y,Result) ). This does not work. I am well aware that X+Y is not evaluated, here, and instead I'd have to do: foreach( between(1,10,X), ( XPlusY is X + Y, somePredicate(X, XPlusY, Result) ) ). Except, that doesn't work, either. As near as I can tell, the scope of XPlusY extends outside of foreach - i.e., XPlusY is 1 + Y, XPlusY is 2

Finding all possible paths in a graph without cycle

丶灬走出姿态 提交于 2019-12-24 06:34:14
问题 I'm trying to write a Prolog program to give me all possible paths between two points in a graph (with cycle). edge(a,b). edge(a,c). edge(a,d). edge(b,e). edge(c,e). edge(c,f). edge(d,f). edge(f,g). edge(g,e). edge(e,a). show_path(X,Y,[X,Y]) :- edge(X,Y). show_path(X,Z,[X|T]) :- edge(X,Y), not(member(Y, T)), show_path(Y,Z,T). I'm trying to use not(member()) to exclude the cycles and avoid infinite loop but it doesn't yield all possible solutions. How can I alter the program to get the all

Prolog Prefix Expression

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 05:53:13
问题 I'm able to get the sum from the prefix expression but whenever I add a list within a list the program doesn't run. expr(Z) --> num(Z). expr(Z) --> [+], num(X), expr(Y), {Z is X+Y}. expr(Z) --> [-], num(X), expr(Y), {Z is X-Y}. num(D) --> [D], {number(D)}. calculate(L, M) :- expr(M, L, []). This works: calculate([+, 2, -, 9, 8], X] but calculate([+, 2, [-, 9, 8]], X] gives false. What do I need in order for it to work list inside of list? 回答1: very simple: ... expr(Z) --> [L], {calculate(L, Z

How to define simple rule in prolog

自古美人都是妖i 提交于 2019-12-24 05:46:24
问题 I want to define the following rule in prolog: when X is thirsty, X should go to the bar. after, I would ask prolog thirsty(X) and prolog should give back go(bar) I have tried it on the following way: go(Y). thirsty(X) :- go(bar). But when I test it with thirsty(bob). The result will only be true . Can someone help me what I have to change to get go(bar) as the result? 回答1: when X is thirsty, X should go to the bar. Looking at the first part of this sentence, "when X is thirsty”, there’s

gprolog - Simple way to determine whether one list is a permutation of another

…衆ロ難τιáo~ 提交于 2019-12-24 05:33:37
问题 I'm trying to write a prolog program that determines whether one list is a permutation of another. Input is of the form perm(L,M) , which will be true if and only if list L is a permutation of list M . This is for my AI class, so I cannot just use the nifty little permutation predicate that gprolog already provides. Our professor noted that the member predicate might be useful, but any ideas I have that involve it seem to require very tricky and not-so-declarative things (and I'm assuming

what does the follow prolog codes do?

こ雲淡風輕ζ 提交于 2019-12-24 05:30:12
问题 I am having trouble understanding the codes below. Can someone explain step by step what is happening if I have the follow input: append([1,2,3], Lst). Actually, I don;t get how 1 and 2 is appended the list Lst as a result. append([_], []). append([H|T], [H|N]) :- append(T,N). 回答1: It sounds like you're new to Prolog. If so, welcome! Let's analyze this. This unfortunately-named function has two clauses. Prolog looks at the clauses in order to see which one applies. When it finds one that

Prolog Tree Traversal

人盡茶涼 提交于 2019-12-24 05:05:47
问题 Good Day, I am trying to write a Prolog program that given a tree with a functor of a: start(a(f,2,9), X). I want it to square any values inside so that it yields: X = a(f,4,81). I have code that squares numbers in a list already that works. Here's what I have so far: start([],[]). start(Tree, []) :- Tree =.. [P|C], write(P), nl, write(C), nl, squareMe([P|C], []). squareMe([X|T], [Y|Result]) :- % I think the problem is here atom(X), Y=X, squareMe(T, Result). squareMe([X|T], [Y|Result]) :-