prolog-dif

How to implement a not_all_equal/1 predicate

拜拜、爱过 提交于 2019-12-07 05:17:58
问题 How would one implement a not_all_equal/1 predicate, which succeeds if the given list contains at least 2 different elements and fails otherwise? Here is my attempt (a not very pure one): not_all_equal(L) :- ( member(H1, L), member(H2, L), H1 \= H2 -> true ; list_to_set(L, S), not_all_equal_(S) ). not_all_equal_([H|T]) :- ( member(H1, T), dif(H, H1) ; not_all_equal_(T) ). This however does not always have the best behaviour: ?- not_all_equal([A,B,C]), A = a, B = b. A = a, B = b ; A = a, B = b

Solving a textual logic puzzle in Prolog - Find birthday and month

你说的曾经没有我的故事 提交于 2019-12-05 21:20:38
I'm reading the "7 Languages in 7 Days"-book, and have reached the Prolog chapter. As a learning exercises I'm trying to solve some textual logic puzzles. The puzzle goes as follow: Five sisters all have their birthday in a different month and each on a different day of the week. Using the clues below, determine the month and day of the week each sister's birthday falls. Paula was born in March but not on Saturday. Abigail's birthday was not on Friday or Wednesday. The girl whose birthday is on Monday was born earlier in the year than Brenda and Mary. Tara wasn't born in February and her

How to implement a not_all_equal/1 predicate

﹥>﹥吖頭↗ 提交于 2019-12-05 10:04:24
How would one implement a not_all_equal/1 predicate, which succeeds if the given list contains at least 2 different elements and fails otherwise? Here is my attempt (a not very pure one): not_all_equal(L) :- ( member(H1, L), member(H2, L), H1 \= H2 -> true ; list_to_set(L, S), not_all_equal_(S) ). not_all_equal_([H|T]) :- ( member(H1, T), dif(H, H1) ; not_all_equal_(T) ). This however does not always have the best behaviour: ?- not_all_equal([A,B,C]), A = a, B = b. A = a, B = b ; A = a, B = b, dif(a, C) ; A = a, B = b, dif(b, C) ; false. In this example, only the first answer should come out,

Check if any element's frequency is above a limit

不羁的心 提交于 2019-12-04 14:41:38
I want to solve a problem that is I have a Prolog list of elements. If the any of the element frequency is greater than N then false is return. My expectation like below. ?- frequency([1,2,2,2,5],3). true. ?- frequency([1,2,2,2,2,5],3). false. I have a code for get particular element frequency. Any idea for the problem. count(_, [], 0) :- !. count(X, [X|T], N) :- count(X, T, N2), N is N2 + 1. count(X, [Y|T], N) :- X \= Y, count(X, T, N). repeat Use clpfd ! :- use_module( library(clpfd) ). If we build on auxiliary predicate list_counts/2 , we can define frequency/2 like this: frequency(Es, M) :

Not member rule doesn't work as expected in Prolog

那年仲夏 提交于 2019-12-02 18:11:50
问题 I am attempting to create a maze program in Prolog, whereby the aim is to find a route from the start of the maze to a point in the centre of the maze called m. The maze consists of squares which are connected using one of four colours: Blue, Green, Purple or Orange. The route from start to the centre follows a repeating pattern of the four colours. I have created the following code: link2(A, Colour, B) :- link(A, Colour, B). link2(A, Colour, B) :- link(B, Colour, A). changecolour(blue,green)

Not member rule doesn't work as expected in Prolog

和自甴很熟 提交于 2019-12-02 09:12:13
I am attempting to create a maze program in Prolog, whereby the aim is to find a route from the start of the maze to a point in the centre of the maze called m. The maze consists of squares which are connected using one of four colours: Blue, Green, Purple or Orange. The route from start to the centre follows a repeating pattern of the four colours. I have created the following code: link2(A, Colour, B) :- link(A, Colour, B). link2(A, Colour, B) :- link(B, Colour, A). changecolour(blue,green). changecolour(green,purple). changecolour(purple,orange). changecolour(orange,blue). route(A, Colour1,

Counting within a list. Help me understand this code

怎甘沉沦 提交于 2019-12-02 04:41:22
问题 I found a 3 year old question that helps me count the number of occurrences of variables within a list. The question had the answer below. The code works. But I can't understand how, can someone help me make sense of this? Here is the answer with the code I found, writing in quotation marks is part of the answer: count([],X,0). count([X|T],X,Y):- count(T,X,Z), Y is 1+Z. count([X1|T],X,Z):- X1\=X,count(T,X,Z). 'However note that the second argument X is supposed to be instantiated. So e.g.

Counting within a list. Help me understand this code

血红的双手。 提交于 2019-12-02 02:51:43
I found a 3 year old question that helps me count the number of occurrences of variables within a list . The question had the answer below. The code works. But I can't understand how, can someone help me make sense of this? Here is the answer with the code I found, writing in quotation marks is part of the answer: count([],X,0). count([X|T],X,Y):- count(T,X,Z), Y is 1+Z. count([X1|T],X,Z):- X1\=X,count(T,X,Z). 'However note that the second argument X is supposed to be instantiated. So e.g. count([2,23,3,45,23,44,-20],23,C) will unify C with 2. If you want the count for every element use' :-

Difference between X\\=Y and dif(X,Y)

旧城冷巷雨未停 提交于 2019-12-01 22:47:56
What is the difference between this: X \= Y and this piece of code: dif(X, Y) I thought that they should behave the same, but they do not. Here's the example: n_puta(L, N, X) :- nputa(L, N, 0, X). nputa([], N, C, _) :- N = C. nputa([G|R], N, C, X) :- G = X, nputa(R, N, Y, X), C is Y - 1. nputa([G|R], N, C, X) :- dif(G,X), nputa(R, N, C, X). And here are some calls: ?- n_puta([a,a,b,b,b], 2, X). X = a ; false. ?- n_puta([a,a,b,a,b,b], 3, X). X = a ; X = b ; false. X should be the atom that occurs exactly N times in the list L. If I replace dif(G, X) with G \= X , I don't get the expected result

How does recursion in Prolog works from inside. One example

巧了我就是萌 提交于 2019-12-01 21:07:41
问题 I've got here a small script, that converts list of elements into a set. For example list [1,1,2,3] -> set [1,2,3]. Can somebody explain to me, step by step, whats happening inside those procedures? Can you please use my [1,1,2,3] -> [1,2,3] example? list_to_set([],[]). list_to_set([A|X],[A|Y]):- list_to_set(X,Y), \+member(A,Y). list_to_set([A|X],Y):- list_to_set(X,Y), member(A,Y). 回答1: The procedural counterpart of a prolog program is called SLDNF. Query: list_to_set([1,1,2,3], Result) Now