prolog

How to sum multiple results from a predicate in prolog?

▼魔方 西西 提交于 2019-12-20 05:51:01
问题 So I have a predicate that sometimes returns more than one numerical result, eg. X=3; X=1 How can I sum multiple return values together? On occasion my predicate returns a single numerical value which is fine but if it returns more than one I want it to sum the multiple results together and output that rather than listing them separately. Thanks. 回答1: You didnt show us code so i cant tell exactly what is correct solution, but you can try with findall/3 or bagof/3 predicates to collect results

Using a list from a fact within Prolog rules

落花浮王杯 提交于 2019-12-20 05:38:29
问题 I am currently writing a rail line program but am having a little trouble using lists that come from facts. I am quite new to Prolog and so far have written the following facts and rules: location(euston, [northernLine]). location(warrenStreet, [victoriaLine, northernLine]). location(warwickAvenue, [bakerlooLine]). location(paddington, [bakerlooLine]). hasCommonLine(Location1, Location2, Line) :- location(Location1, Line), location(Location2, Line). The idea is for the rule to return the name

prolog replacing elements in list

青春壹個敷衍的年華 提交于 2019-12-20 05:34:08
问题 I need help with understanding how to work with lists like that [a(b,c),a(x,d)] change(S,K,R) changes first list [a,c,b] with values given in second list [c(a,x),c(b,y)] ?- change([a,c,b],[k(a,x),k(b,y)],R). R = [x,c,y]. % my program but it works with second list that is of wrong list elements type k(a,x) but like [a,x] and works kinda poorly cause return True instead of R= x,c,y, if i print R value it is [y,c,x|_2826] i call my code with ?- change([a,c,b],[a,x,b,y],R). change([],[],[]):-!.

how to pass a list in a predicate in prolog

≡放荡痞女 提交于 2019-12-20 05:23:12
问题 I want to store a paragram as a list in a variable and then call that list for counting how many times a particular word appears in that paragraph. However, when I do this: L = [hello,hello,hello]. counthowmany(_, [], 0) :- !. counthowmany(X, [X|Q], N) :- !, counthowmany(X, Q, N1), N is N1+1. counthowmany(X, [_|Q], N) :- counthowmany(X, Q, N). ... and compile buffer, and then ask this: counthowmany(hello,L,N). The number of "hello" occurrences in the list doesn't show, instead I receive a

json get prolog predicate

删除回忆录丶 提交于 2019-12-20 05:00:24
问题 I'm tryung to create this predicate in prolog: The predicate json_get/3 can be defined as: json_get(JSON_obj, Fields, Result). which is true when Result is recoverable by following the chain of fields in Fields (a list) starting from JSON_obj . A field represented by N (with N a major number o equal to 0) corresponds to an index of a JSON array. Please help me to understand to follow the chain of fields. Thanks edit1: Of course, so json object looks like this '{"name" : "Aretha", "surname" :

Replacing elements in list of lists PROLOG

淺唱寂寞╮ 提交于 2019-12-20 04:33:13
问题 I've developed a predicate which replaces the value of the index Index of a list List with Value and creates a new updated list NewList . %replace(List,Index,Value,NewList) replace([_|T], 0, X, [X|T]). replace([H|T], I, X, [H|R]):- I > -1, NI is I-1, replace(T, NI, X, R), !. replace(L, _, _, L). The predicate works fine on regular lists, but I want to make it work on a list of lists and I am kind of stuck on a little step. subs([]). subs([Head|Tail], Index) :- replace((Head), Index, 'r',

Generating subsets using length/2 and ord_subset/2

我的未来我决定 提交于 2019-12-20 04:32:40
问题 I am a beginner in prolog. I tried this in swipl interpreter: ?- length(Lists, 3), ord_subset(Lists, [1, 2, 3, 4]). false. expecting to get all length-3 lists that are subsets of [1, 2, 3, 4] like [1, 2, 3] or [1, 2, 4]. Why do i get false? Notice: both length and ord_subset are builtin functions (or whatever they are called) in SWI-Prolog. 回答1: You don't get a solution because the ord_subset/2 predicate only checks if a list is a subset of another list; it does not generate subsets. Here is

Generating subsets using length/2 and ord_subset/2

两盒软妹~` 提交于 2019-12-20 04:32:07
问题 I am a beginner in prolog. I tried this in swipl interpreter: ?- length(Lists, 3), ord_subset(Lists, [1, 2, 3, 4]). false. expecting to get all length-3 lists that are subsets of [1, 2, 3, 4] like [1, 2, 3] or [1, 2, 4]. Why do i get false? Notice: both length and ord_subset are builtin functions (or whatever they are called) in SWI-Prolog. 回答1: You don't get a solution because the ord_subset/2 predicate only checks if a list is a subset of another list; it does not generate subsets. Here is

AVL Binary Heap(Balanace test)

孤人 提交于 2019-12-20 04:25:07
问题 I'm trying to achieve a testing if a tree is AVL tree or not using prolog. I've made a height test that works for the tests I've done so far but my balancing test is still not going strong. This is my work so far: avl('Branch'(LeftBranch,RightBranch)) :- height(LeftBranch,H1), height(RightBranch,H2), abs(H1-H2) =< 1. I've based this code from an older stackoverflow code. But it doesn't work in all cases. Will include my height code. Somewhere I've made a misstake and Im sure where to find it.

Prolog search for possible combination for subtracting 2 elements from a list

江枫思渺然 提交于 2019-12-20 04:07:51
问题 This is an extended problem from this page. Prolog possible removal of elements in a list For example, for a list X = [1,2,3], I can subtract like following: subtract 1 from 1st / 2nd element, and X becomes [1-1, 2-1, 3] = [0, 1, 3] = [1, 3] subtract 1 from 1st / 3rd element: X becomes [2, 2] subtract 1 from 2nd / 3rd element: X becomes [1, 1, 2] subtract 2 from 2nd / 3rd element: X becomes[1, 1] So, it is always subtracting 2 elements, and with the same number, but always a real number. Have