prolog-findall

Sum up data from facts

空扰寡人 提交于 2020-04-18 12:31:17
问题 (Given a list of movies, write a PROLOG rule to add and display the total takings.) This is my question I am basically trying to add an integer value give a list of movies from the list below. I am quite new in Prolog and I don't really understand how things work. takings(air_force_one,315000000). takings(american_beauty,336000000). takings(american_pie,201700000). takings(american_wedding,230700000). takings(armageddon,554600000). takings(as_good_as_it_gets,313300000). takings(austin_powers

Prolog findall/3 rule convert to recursive rule

拥有回忆 提交于 2020-03-16 06:38:12
问题 score(Movies,Total) :- findall(Money,(member(Movie,Movies),takings(Movie,Money)),Profit), sum_list(Profit,Total) I want to convert this rule using recursion but I am not sure how. Help! Link to the previous question that answers this one : Sum up data from facts 回答1: % base case score([],0). % recursive case score([Movie|Movies],Total) :- takings(Movie,Profit), score(Movies,Total0), Total is Total0 + Profit. Example run ?- score([robots,hulk,bad_boys_ii],Y). Y = 749200000. Explanation of code

Prolog - Operation inside findall

旧时模样 提交于 2019-12-23 22:11:21
问题 Using a findall in Prolog how can I perform operations inside the goal without affecting backtracking? The following example explains what I'm trying to achieve: value('M1', 11, 3). value('M2', 11, 3). connection('M1',1, 'A', 'B'). connection('M1',1, 'B', 'C'). connection('M1',2, 'C', 'D'). connection('M1',2, 'D', 'E'). connection('M2',1, 'D', 'F'). run :- bbR('C',[(0,'X',['A'])],_,_). run2 :- bbR2('C',[(0,['A'])],_,_). bbR(Destination,[(Cost,_,[Destination|T])|_],Result,Cost):- reverse(

findall/3 creates new, unrelated variables in its resulting list

半世苍凉 提交于 2019-12-23 07:57:20
问题 ?- permutation([A,B,C],Z). Z = [A, B, C] ; Z = [A, C, B] ; Z = [B, A, C] ; Z = [B, C, A] ; Z = [C, A, B] ; Z = [C, B, A] ; false. Makes sense. I can work on a permutation of [A,B,C] and that permutation contains the same elements as in [A,B,C] , so everything I do to those elements will apply to my original list. Now: ?- findall(X, permutation([A,B,C], X), Z). Z = [[_G1577, _G1580, _G1583], [_G1565, _G1568, _G1571], [_G1553, _G1556, _G1559], [_G1541, _G1544, _G1547], [_G1529, _G1532, _G1535],

Prolog findall Implementation

﹥>﹥吖頭↗ 提交于 2019-12-17 09:59:58
问题 I've been tasked to implement a version of findall in Prolog without using any Prolog built-ins except for not and cut - so basically in pure Prolog. I'm trying to search a tree for all direct descendants and return the results in a list parent(a, b). parent(b, c). parent(b, d). parent(e, d). What I have so far is: find(X, L) :- find2(X, [], L). find2(X, Acc, L) :- parent(Y, X), find2(Y, [Y|Acc], L). find2(_, Acc, Acc). What I want to be getting when I enter for example: find(a,X). would be:

Prolog findall infinite loop

左心房为你撑大大i 提交于 2019-12-13 22:06:13
问题 I have a predicate that asserts things. When I use a findall it goes into an infinite loop. assertarv([N|R]):- assert(veiculos_troncos(N)), assertarv(R). assertarv([]). When I use findall(A,veiculos_troncos(A),NTr) , after having used the above predicate to assert many points (coordenates). This is what I get(using findall): Exit: (22) veiculos_troncos((-7, 6)) ? creep Redo: (22) veiculos_troncos(_G6719) ? creep Exit: (22) veiculos_troncos((-6, 6)) ? creep Redo: (22) veiculos_troncos(_G6719)

Correct use of findall/3, especially the first template argument

橙三吉。 提交于 2019-12-13 16:52:28
问题 i know there is a build-in function findall/3 in prolog, and im trying to find the total numbers of hours(Thrs) and store them in a list, then sum the list up. but it doesnt work for me. here is my code: totalLecHrs(LN,THrs) :- lecturer(LN,LId), findall(Thrs, lectureSegmentHrs(CC,LId,B,E,THrs),L), sumList(L,Thrs). could you tell me what's wrong with it? thanks a lot. 回答1: You need to use a "dummy" variable for Hours in the findall/3 subgoal. What you wrote uses THrs both as the return value

How to inline a goal with findall/3, (use just one predicate)?

旧巷老猫 提交于 2019-12-13 02:12:17
问题 I have a knowledgebase that looks something like this fact1(1, _, a, _, _). fact1(2, _, c, _, _). fact1(3, _, d, _, _). fact1(4, _, f, _, _). fact2(_, 1, b, _, _). fact2(_, 2, c, _, _). fact2(_, 4, e, _, _). For every fact1 & fact2 , where (in this example) the numbers match up, I want to have a list of the corresponding letters as tuples. I would like to use findall/3 and only one predicate for this. I have asked a question here before on how to solve something similar, where the answer was

Can you use clpfd to implement a coverage algorithm?

自作多情 提交于 2019-12-11 03:54:24
问题 Say I want to find the set of features/attributes that differentiate two classes in a simple matching manner can I use clpfd in prolog to do this? c_s_mining(Features,Value):- Features = [F1,F2,F3,F4], Features ins 0..1, ExampleA = [A1,A2,A3,A4], ExampleB =[B1,B2,B3,B4], ExampleC =[C1,C2,C3,C4], A1 #=0, A2#=1,A3#=0,A4#=1, B1 #=0, B2#=1,B3#=0,B4#=1, C1 #=1, C2#=0,C3#=0,C4#=1, ExampleD =[D1,D2,D3,D4], ExampleE =[E1,E2,E3,E4], ExampleQ =[Q1,Q2,Q3,Q4], D1#=1,D2#=0,D3#=1,D4#=0, E1#=1,E2#=0,E3#=1

Correct use of findall/3, especially the last result argument

倖福魔咒の 提交于 2019-12-02 06:46:07
问题 I'm a beginner in Prolog and I am dealing with a problem that might seem stupid to you, but I really can't understand what I'm doing wrong! Ok, I have this file fruits.pl and inside that I have something like this: fruit(apple,small,sweet). fruit(lemon,small,nosweet). fruit(melon,big,sweet). I have already (inside that file made a coexist(X,Y) atom that checks if two fruits can be put together in a plate. It works fine! But now I can't create a suggest(X) that takes as a parameter a fruit and