prolog-dif

Counting occurrences in list

雨燕双飞 提交于 2019-12-11 10:55:35
问题 I'm trying to create a rule that counts the number of occurrences of a certain element in a given list, what I've tried so far does not seem to work the way I expect: The first argument here should be the list, the second one the element we are looking for, and the last one the number of occurrences: %empty list should always return 0 occurences count([],E,0) :- true. %if our head is what we are looking for, count count([E|T],E,N) :- count(T,E,N-1). %otherwise, do not count count([H|T],E,N) :

Trying to obtain cost for a given path

廉价感情. 提交于 2019-12-11 01:32:56
问题 I am new to Prolog I am trying in Prolog a rule that gives me a given path from a node to another and also gives me the total weight of the path. I have succeeded to get all the edges of the path but I am not able to show the weight of the path. I debbuged it and it is seen that variable S adds up to the whole weight of the path but in the way back, deletes all the elements. My idea is to add the total weight to P. Code: notIn(A,[]). notIn(A,[H|T]):- A\==H,notIn(A,T). path(X,X,_,[], S, P).

Prolog Query returning H128

本小妞迷上赌 提交于 2019-12-10 22:48:02
问题 In the query below, firstly I'm getting X = H128 , where does that come from? Also why is it returning yes? Is it because the variable X is actually not defined and we are testing for that condition? ?- not(X==3). X = H128 yes 回答1: Your query is using an uninstantiated variable (X). When checking whether X is instantiated with the term 3 it (X==3) it fails because X is uninstantiated. Therefore, not(X==3) will succeed as the prolog engine cannot prove X==3. Your prolog interpreter is thus

Handling prolog context free grammar

我的梦境 提交于 2019-12-10 19:33:25
问题 Given a CFG S --> a S b | c | d I wanna write a predicate like, grammar('S', sentence) which generates all possible sentences like sentence=acb, sentence=acd, sentence=c, sentence=ab...................... Using left most derivation , if the encountered symbol is terminal it should print out that terminal, and if the encountered symbol is non terminal 'S' , it should backtrack and substitute and one of the grammar a S b or c or d and repeat the process. I dont want any code...just help me with

Add two more occurrences using prolog

耗尽温柔 提交于 2019-12-10 13:47:53
问题 I have a list [a, b, a, a, a, c, c] and I need to add two more occurrences of each element. The end result should look like this: [a, a, a, b, b, b, a, a, a, a, a, c, c, c, c] If I have an item on the list that is the same as the next item, then it keeps going until there is a new item, when it finds the new item, it adds two occurrences of the previous item then moves on. This is my code so far, but I can't figure out how to add two... dbl([], []). dbl([X], [X,X]). dbl([H|T], [H,H|T], [H,H|R

Simplified Travelling Salesman in Prolog

大憨熊 提交于 2019-12-10 02:33:59
问题 I've looked through the similar questions but can't find anything that's relevant to my problem. I'm struggling to find an algorithm or set of 'loops' that will find a path from CityA to CityB , using a database of distance(City1,City2,Distance) facts. What I've managed to do so far is below, but it always backtracks at write(X), and then completes with the final iteration, which is what I want it to do but only to a certain extent. For example, I don't want it to print out any city names

Prolog: inequality operator

允我心安 提交于 2019-12-08 19:47:34
问题 I am using SICStus Prolog and have a set of facts: student('John Henry', 'Maths'). student('Jim Henry', 'Maths'). student('John Alan', 'Maths'). student('Alan Smith', 'Computing'). student('Gary Henry', 'Maths'). I want to get the shared subject of two students where both students are different, so I got: sharedSubject(S1, S2, Sub) :- S1 \== S2, student(S1, Sub), student(S2, Sub). However, when I enter: sharedSubject('John Henry', F, E). I get F = 'John Henry' . Can someone point out where I

How to validate commutativity involving the dif/2 constraint?

前提是你 提交于 2019-12-08 19:14:02
问题 There is a lot of hype around the dif/2 constraint, especially as a rescue for some non-declarativity of (\=)/2 and (\==)/2. This non-declarativity is often characterized as non-monotonicity and examples of non-communtativity are given. But what would be the means to test whether test cases involving dif/2 are commutative. Here is a meta explanation what I want to do: I make a commutativity test, and I want to probe that both variants give the same result: ?- A, B. -- versus -- ?- B, A. So

Guard clauses in prolog?

拜拜、爱过 提交于 2019-12-08 16:23:12
问题 Do they exist? How are they implemented? The coroutining predicates of SWI-Prolog ( freeze , when , dif etc.) have the functionality of guards. How do they fit in the preferred Prolog programming style? I am very new to logic programming (with Prolog and altogether) and somewhat confused by the fact that it is not purely declarative, and requires procedural considerations even in very simple cases (see this question about using \== or dif). Am I missing something important? 回答1: First a

Remove leading zeros in list in Prolog

柔情痞子 提交于 2019-12-08 15:00:33
问题 I have a list with an unknown number of zeros at the beginning of it, for example [0, 0, 0, 1, 2, 0, 3]. I need this list to be stripped of leading zeros, so that it would look like [1, 2, 0 , 3]. Here's what I have: lead([Head | _], _) :- Head =\= 0. lead([0 | Tail], _) :- lead(Tail, Tail). The output of which is simply True. Reading the trace shows that it is running until it has a list with no leading zeros, but then the answer doesn't propagate back up the stack. I'm pretty new to Prolog,