prolog

What does the s() predicate do in Prolog?

匆匆过客 提交于 2019-12-21 11:55:07
问题 I have been trying to learn Prolog, and am totally stumped on what the predicate s() does. I see it used often and there is so little resources on the internet about Prolog that I cannot find an answer. Ex. /* sum(Is,S) is true if S is the sum of the list of integers Is. */ sum([],0). sum([0|Is],S):-sum(Is,S). sum([s(I)|Is], s(Z) ):-sum([I|Is],Z). 回答1: s/1 does not do anything in itself, and it's not really a predicate. They are just terms, a representation of the successor of their argument.

minimax for tic-tac-toe

余生长醉 提交于 2019-12-21 11:24:08
问题 I'm trying to solve tic-tac-toe with a simple minimax algorithm. Simple, but should cover a lot of the language. What I have so far: The board is represented as an array of 9 (unbound) variables, that are either set to x or o . The win condition is then basically: win(Player, [X1,X2,X3|_]) :- X1==Player,X2==Player,X3==Player. etc for all eight variants. draw is just a simple check whether all variables are bound. The move clauses are also simple: move(Player, [X|_], 0, 0) :- var(X), X=Player.

Possible behaviors of `predsort/3`

你离开我真会死。 提交于 2019-12-21 10:40:34
问题 This is a followup to an answer to a question about sorting on a particular argument of a term, without creating a new list for a keysort (if I understood the original question correctly). Say we wanted predsort/3 to behave exactly as sort/2 : if I understand correctly, this would mean calling it as: ?- predsort(compare, List, Sorted). Now say that we wanted to use predsort/3 to sort as implemented by msort/2 (see also this question). One way to do it would be to define a comparison predicate

Simple prolog program. Getting error: >/2: Arguments are not sufficiently instantiated

此生再无相见时 提交于 2019-12-21 08:01:10
问题 I made a Prolog predicate posAt(List1,P,List2) that tests whether the element at position P of List1 and List2 are equal: posAt([X|Z], 1, [Y|W]) :- X = Y. posAt([Z|X], K, [W|Y]) :- K > 1, Kr is K - 1, posAt(X, Kr, Y). When testing: ?- posAt([1,2,3], X, [a,2,b]). I expected an output of X = 2 but instead I got the following error: ERROR: >/2: Arguments are not sufficiently instantiated Why am I getting this error? 回答1: A Prolog predicate is a relation between arguments, and your statement the

Numbers in a list smaller than a given number

旧巷老猫 提交于 2019-12-21 07:41:21
问题 xMenores(_,[],[]). xMenores(X,[H|T],[R|Z]) :- xMenores(X,T,Z), X > H, R is H. xMenores takes three parameters: The first one is a number. The second is a list of numbers. The third is a list and is the variable that will contain the result. The objective of the rule xMenores is obtain a list with the numbers of the list (Second parameter) that are smaller than the value on the first parameter. For example: ?- xMenores(3,[1,2,3],X). X = [1,2]. % expected result The problem is that xMenores

Numbers in a list smaller than a given number

喜夏-厌秋 提交于 2019-12-21 07:41:21
问题 xMenores(_,[],[]). xMenores(X,[H|T],[R|Z]) :- xMenores(X,T,Z), X > H, R is H. xMenores takes three parameters: The first one is a number. The second is a list of numbers. The third is a list and is the variable that will contain the result. The objective of the rule xMenores is obtain a list with the numbers of the list (Second parameter) that are smaller than the value on the first parameter. For example: ?- xMenores(3,[1,2,3],X). X = [1,2]. % expected result The problem is that xMenores

Prolog gives error “undefined procedure” when trying to use :-

六眼飞鱼酱① 提交于 2019-12-21 07:16:30
问题 I'm using SWI-Prolog on Windows and am getting the following error: 14 ?- parent(X, Y) :- child(Y, X). ERROR: toplevel: Undefined procedure: (:-)/2 (DWIM could not correct) I'm not entirely sure what's going on, as this worked last week and I am just starting to learn Prolog. 回答1: The FAQ says it all: http://www.swi-prolog.org/FAQ/ToplevelMode.html You need to create a file and write your program with rules there. The top level command line will only allow you to issue queries. 回答2: You can

singleton variables in prolog

南楼画角 提交于 2019-12-20 17:27:42
问题 I was testing my new version of SWI prolog and keep coming across the error : singleton variable . Example: member(X,[X|T]). member(X,[X|T]) :- member(X,T). finds the member of a list such as : member(yolands,[yolanda,tim]) X = yes but instead I get a singleton variables error for X and T if I do the following: member(X,[X|_]). member(X,[_|T]) :- member(X,T). It works but looks ugly! Can anyone explain why single variables ar enot allowed and if this ANSI standard? 回答1: Singleton variables

Reversing a List in Prolog

*爱你&永不变心* 提交于 2019-12-20 14:24:40
问题 I have finished a homework assignment for my programming class. I was supposed to create a Prolog program that reverses a list. I, however, am having trouble understanding why exactly it works. %1. reverse a list %[a,b,c]->[c,b,a] %reverse(list, rev_List). reverse([],[]). %reverse of empty is empty - base case reverse([H|T], RevList):- reverse(T, RevT), conc(RevT, [H], RevList). %concatenation What exactly is RevT in this case? I know it is supposed to represent the reverse of T or the rest

Reversing a List in Prolog

假装没事ソ 提交于 2019-12-20 14:24:15
问题 I have finished a homework assignment for my programming class. I was supposed to create a Prolog program that reverses a list. I, however, am having trouble understanding why exactly it works. %1. reverse a list %[a,b,c]->[c,b,a] %reverse(list, rev_List). reverse([],[]). %reverse of empty is empty - base case reverse([H|T], RevList):- reverse(T, RevT), conc(RevT, [H], RevList). %concatenation What exactly is RevT in this case? I know it is supposed to represent the reverse of T or the rest