prolog

Prolog Clear List of negative elements without using cuts

不羁岁月 提交于 2019-12-19 11:57:49
问题 How do I write a procedure in Prolog that clears a list of integers of its negative elements and returns the result in a new list? Without using cuts but can use negation. For example: ?- filter([1,0,-6,7,-1],L). L = [1,0,7]; no 回答1: You have it almost right. Your solution was: filter([],[]). filter([H|T],S) :- H<0, filter(T,S). filter([H|T],S) :- H>=0, filter(T,[H|S]). The base case and the case where the item is negative are right. The problem is with the last case. Once you checked that

Prolog: why my predicate returns false?

非 Y 不嫁゛ 提交于 2019-12-19 11:35:10
问题 so I wrote a predicate that counts how many times an element occurs in a list of lists. count([], _, 0). #base case count([[Elem|Rest]|OtherLists], Elem, Count) :- #Elem is the head of sublist !, count([Rest|OtherLists], Elem, NewCount), succ(NewCount, Count). count([[_|Rest]|OtherLists], Elem, Count) :- #Elem is not the head of sublist count([Rest|OtherLists], Elem, Count). count([[]|OtherLists], Elem, Count) :- #Head sublist is an empty list count(OtherLists, Elem, Count). Now that if I

Check if variable is empty or filled

牧云@^-^@ 提交于 2019-12-19 10:36:12
问题 I have the following problem: prolog prog: man(thomas, 2010). man(leon, 2011). man(thomas, 2012). man(Man) :- once(man(Man, _). problem: ?- man(thomas). true ; %i want only on true even if there are more "thomas" *working because of once()* ?- man(X). X = thomas ; %i want all man to be listed *isn't working* goal: ?- man(thomas). true ; ?- man(X). X = thomas ; X = leon ; X = thomas ; I do unterstand why this happens, but still want to get the names of all man. So my solution woud be to look

Getting last char of a string in Prolog

*爱你&永不变心* 提交于 2019-12-19 09:49:21
问题 I need to get the last char of a string. For example: ?- last_char('abde', X). X = 'e' ?- last_char('abdef', X). X = 'f' Could someone help me, please? I'm new to Prolog. 回答1: with a little help from sub_atom/5, a really handy ISO builtin: ?- sub_atom(abdef, _, 1, 0, C). C = f. 回答2: You could use name and reverse to convert the string to a list and back: last_char(S, X) :- name(S, N), reverse(N, [F|_]), name(X, [F]). Depending on your Prolog version you might have to import a list library for

Prolog arithmetic syntax

荒凉一梦 提交于 2019-12-19 09:15:13
问题 How to define a as a integer/float number ? I want to find the results of a+b+c+d=10 where a,b,c,d is integer and >=0 . 回答1: Here is a simple, modern, pure Prolog, non-CLP-library solution: range(X):- member(X,[0,1,2,3,4,5,6,7,8,9,10]). ten(A,B,C,D):- range(A), range(B), range(C), range(D), 10 =:= A + B + C + D. 回答2: with SWI-Prolog you can use CLP(FD) library 1 ?- use_module(library(clpfd)). % library(error) compiled into error 0.00 sec, 9,764 bytes % library(clpfd) compiled into clpfd 0.05

Make a predicate reversible

自闭症网瘾萝莉.ら 提交于 2019-12-19 08:05:18
问题 I'm new to prolog; I'm coming from a structured programming background, as will become obvious :) I am building up a prolog query that involves reversing a number; eg. reverse_num(123,X) results in X = 321 . I came up with the following definition, but it only works when I provide a number as the first parameter. reverse_num(Num, Revnum) :- number_chars(Num, Atoms), reverse(Revatoms, Atoms), number_chars(Reversed, Revatoms), Reversed = Revnum. the number_chars/2 predicate doesn't like an

Why does SWI-Prolog only give me the first answer?

我怕爱的太早我们不能终老 提交于 2019-12-19 05:59:24
问题 I'm new to Prolog. I'm just trying simple examples to learn. I have this .pl file with these lines: parent(pam,bob). parent(tom,bob). parent(tom,lio). parent(bob,ann). parent(bob,pat). parent(pat,jim). After consulting and testing, it only shows the first answer. For example: 5 ?- parent(X,Y). X = pam, Y = bob . Isn't it supposed to give all the combinations that satisfy the relation parent ? Do anyone have idea what the problem is? 回答1: don't hit enter after your first results shows, use

prolog, find list elements in a list of tuples

感情迁移 提交于 2019-12-19 05:48:28
问题 I'm trying to solve a new program with Prolog, and I'm stuck, and don't know how to continue... I must do a predicate that has 3 arguments, the first is a list of elements, the second one is a list of tuples, and the third one must be a list returned that contains the second element of the tuples, if the first element of the tuple match with an element of the first argument list. It must delete copies also!! For example, check([a,c],[(a,aa),(bb,bbb),(a,aa),(c,def)],X). X = [aa, def] . As you

Parsing numbers with multiple digits in Prolog

被刻印的时光 ゝ 提交于 2019-12-19 05:29:45
问题 I have the following simple expression parser: expr(+(T,E))-->term(T),"+",expr(E). expr(T)-->term(T). term(*(F,T))-->factor(F),"*",term(T). term(F)-->factor(F). factor(N)-->nat(N). factor(E)-->"(",expr(E),")". nat(0)-->"0". nat(1)-->"1". nat(2)-->"2". nat(3)-->"3". nat(4)-->"4". nat(5)-->"5". nat(6)-->"6". nat(7)-->"7". nat(8)-->"8". nat(9)-->"9". However this only supports 1-digit numbers. How can I parse numbers with multiple digits in this case? 回答1: Use accumulator variables, and pass

Natural number in SWI-prolog & recursive procedure

99封情书 提交于 2019-12-19 04:33:34
问题 I have the next procedure for natural number is SWI-prolog: natural_number(0). natural_number(s(X)) :- natural_number(X). Now I want to do a recursive call, that stop when we arrive to 0. My natural number is represented by - s(0)=0, s(s(0))=1, s(s(s(0)))=2, etc So I define: recommend(A, B, natural_number(0)) :- dosomeFINITEfunction (a,b). recommend(a,b,mynumber):- dosomeFINITEfunction(a,b), recommend (a,b, natural_number(mynumber)). and call with: 3,5,s(0). But it gives me the error: out of