clpfd

Prolog - Arguments are not sufficiently instantiated

荒凉一梦 提交于 2019-11-30 00:43:42
问题 I am writing a little program which counts how many elements in a list are not numbers. Here is my code: not_number([],0). not_number([X|T],R):- not(number(X)), R1 is R+1, not_number(T,R1). not_number([_|Tail],Result):- not_number(Tail,Result). If I execute code like this : ?- not_number([1,2,3,5], R). I am getting that R = 0 (as it should be) R = 0. But if I put a character in the list: ?- not_number([1,2,3,5,a], R). then I am getting this error: ERROR: not_number/2: Arguments are not

I'm curious if Logic Programs can do algebra

↘锁芯ラ 提交于 2019-11-29 18:12:08
问题 I read a brief article about Prolog and Logic Programming. I'm curious if Logic Programs can do algebra. Like would you be able to ask what the Variable of X is in the equation 5+X = 7 and get an answer of -2? 回答1: All serious Prolog systems provide constraint logic programming over finite domains, called CLP(FD) for short, with which you can solve many such equations easily. For example, with SICStus Prolog, SWI and Yap: ?- use_module(library(clpfd)). true. ?- 5+X #= 7. X = 2. Apparently,

Prolog Beginner: How to make unique values for each Variable in a predicate

混江龙づ霸主 提交于 2019-11-29 15:38:45
I have a prolog predicate: Add( [A|B] , Answer ) :- ... ~ Add everything in the list to come up with answer ... I would now like to implement AddUnique that would return unique values for everything in the list except when I give it the variable twice. Here are somethings that are logically equivalent: ?- AddUnique([A, B, C], 10). is equivalent to: ?- Add([A, B, C], 10), A != B, B != C, A != C. And: ?- AddUnique([A, B, B], 10). is equivalent to: ?- Add([A, B, B], 10), A != B. Also: ?- AddUnique([A, B, B], 10). is NOT equivalent to: ?- Add([A, B, B], 10), A != B, B!=B. If ?- AddUnique([A,B,C,D]

I want to count the occurrences of an element in a list

寵の児 提交于 2019-11-29 15:31:19
I want to count the occurrences of an element in a list, and if there is one then the predicate unique would be true, else false. However, if the element occurs more than once, Prolog finds it true. I don't know what to do... count([], X, 0). count([X|T], X, Y) :- count(T, X, Z), Y is 1+Z, write(Z). count([_|T], X, Z) :- count(T, X, Z). unique(St, [Y|RestList]) :- count([Y|RestList], St, N), N =:= 1. false The solution works as far as the first argument is a ground list. In some other cases, it is incorrect: ?- count([E], a, 0). false. Here we ask How must the element E of a list of length 1

Simple nth1 predicate in Prolog

僤鯓⒐⒋嵵緔 提交于 2019-11-29 14:51:46
With SWI Prolog, there's a predicate that finds the nth item in a list called nth1. I want to implement my own version of the predicate but SWI's is so complicated if you look at the listing(nth1) code. Is there a simpler way of doing it? Thank you :). The SWI code is a bit complex because the predicate can be used to generate from a variable index: ?- nth1(Idx,[a,b,c],X). Idx = 1, X = a ; Idx = 2, X = b ; Idx = 3, X = c ; false. If you don't want that behavior, nth1/3 can be implemented easily in terms of nth0 : nth1(Idx,List,X) :- Idx0 is Idx-1, nth0(Idx0,List,X). Edit : it's also possible

prolog convert numbers into roman numerals

杀马特。学长 韩版系。学妹 提交于 2019-11-29 14:00:19
i have this code that converts integers into roman numerals i need to add a function that compares an integer with a roman numeral input and show if it's try or false, for example: roman(v,5). true toroman(0). toroman(N) :- N < 4, put("I"), M is N - 1, toroman(M). toroman(N) :- N = 4, put("I"), put("V"). toroman(N) :- N = 5, put("V"). toroman(N) :- N < 9, put("V"), M is N - 5, toroman(M). toroman(N) :- N = 9, put("I"), put("X"). toroman(N) :- N < 40, put("X"), M is N - 10, toroman(M). toroman(N) :- N < 50, put("X"), put("L"), M is N - 40, toroman(M). toroman(N) :- N < 90, put("L"), M is N - 50

Inverse factorial in Prolog

谁说胖子不能爱 提交于 2019-11-29 13:14:31
Can someone helping me to find a way to get the inverse factorial in Prolog... For example inverse_factorial(6,X) ===> X = 3 . I have been working on it a lot of time. I currently have the factorial, but i have to make it reversible. Please help me. Prolog's predicates are relations, so once you have defined factorial, you have implicitly defined the inverse too. However, regular arithmetics is moded in Prolog, that is, the entire expression in (is)/2 or (>)/2 has to be known at runtime, and if it is not, an error occurs. Constraints overcome this shortcoming: :- use_module( library(clpfd) ).

Understanding CLP(FD) Prolog code of N-queens problem

血红的双手。 提交于 2019-11-29 08:47:45
I am trying to understand N-queens problem's solution as given below: :- use_module(library(clpfd)). n_queens(N, Qs) :- length(Qs, N), Qs ins 1..N, safe_queens(Qs). safe_queens([]). safe_queens([Q|Qs]) :- safe_queens(Qs, Q, 1), safe_queens(Qs). safe_queens([], _, _). safe_queens([Q|Qs], Q0, D0) :- Q0 #\= Q, abs(Q0 - Q) #\= D0, D1 #= D0 + 1, safe_queens(Qs, Q0, D1). I am not able to understand the below snippet: safe_queens([]). safe_queens([Q|Qs]) :- safe_queens(Qs, Q, 1), safe_queens(Qs). safe_queens([], _, _). safe_queens([Q|Qs], Q0, D0) :- Q0 #\= Q, abs(Q0 - Q) #\= D0, D1 #= D0 + 1, safe

List Length in Prolog

随声附和 提交于 2019-11-29 00:37:04
I am beginner in Prolog programming. I wrote this program to calculate the length of a list. Why is below program wrong? length(0, []). length(L+l, H|T) :- length(L, T). I wrote below program and it works correctly. length([], 0). length([H|T], N) :- length(T, N1), N is N1+1. when I changed the order, I got an error. Why? length([], 0). length([H|T], N) :- N is N1+1, length(T, N1). this code length_1(0,[]). length_1(L+1, [H|T]) :- length_1(L,T). works (please note the added square braces), but in unexpected way. It will build an expression tree, that could be evaluated later: ?- length_1(X, [1

Tennis match scheduling

给你一囗甜甜゛ 提交于 2019-11-28 16:39:49
There are a limited number of players and a limited number of tennis courts. At each round, there can be at most as many matches as there are courts. Nobody plays 2 rounds without a break. Everyone plays a match against everyone else. Produce the schedule that takes as few rounds as possible. (Because of the rule that there must a break between rounds for everyone, there can be a round without matches.) The output for 5 players and 2 courts could be: | 1 2 3 4 5 -|------------------- 2| 1 - 3| 5 3 - 4| 7 9 1 - 5| 3 7 9 5 - In this output the columns and rows are the player-numbers, and the