difference-lists

Perfoming member check on a difference list, but how?

一曲冷凌霜 提交于 2020-05-30 23:28:15
问题 I tried to answer another question (wrongly though) and this led to a question on "difference lists" (or "list differences", which seems a more appropriate name, unless "Escherian Construction" isn't preferred) We have a fully ground list of elements obj(X,Y) (both X and Y ground). We want to retain only the first obj(X,_) where X hasn't been encountered yet when going through the list front to back. Those "first elements" must appear in order of appearance in the result. Let's specify the

Perfoming member check on a difference list, but how?

六眼飞鱼酱① 提交于 2020-05-30 23:27:49
问题 I tried to answer another question (wrongly though) and this led to a question on "difference lists" (or "list differences", which seems a more appropriate name, unless "Escherian Construction" isn't preferred) We have a fully ground list of elements obj(X,Y) (both X and Y ground). We want to retain only the first obj(X,_) where X hasn't been encountered yet when going through the list front to back. Those "first elements" must appear in order of appearance in the result. Let's specify the

Perfoming member check on a difference list, but how?

邮差的信 提交于 2020-05-30 23:26:26
问题 I tried to answer another question (wrongly though) and this led to a question on "difference lists" (or "list differences", which seems a more appropriate name, unless "Escherian Construction" isn't preferred) We have a fully ground list of elements obj(X,Y) (both X and Y ground). We want to retain only the first obj(X,_) where X hasn't been encountered yet when going through the list front to back. Those "first elements" must appear in order of appearance in the result. Let's specify the

What does the “-” symbol mean in Prolog when dealing with lists?

喜夏-厌秋 提交于 2020-01-14 07:21:46
问题 I was reading the answer to this question, p(X) :- read(A), q(A,X-[]). q(end,X-X) :- !. q(A,[A|X]-Y) :- read(B), q(B,X-Y). The code above uses the syntax List-List . I somewhat understand what is going on, but I want to know what exactly what the "-" symbol/predicate does here. Also, is this SWI specific? 回答1: The (-)/2 to represent difference lists is a rather uncommon convention. In older books, another operator (\)/2 was used too. Many prefer to use two separate arguments instead. There

Why prolog outputs a weird tree-like list?

六眼飞鱼酱① 提交于 2019-12-11 13:49:15
问题 In this Prolog code I intend to list the first N primes, (...) biggerPrime(N,P) :- isPrime(N), P is N, !. biggerPrime(N,P) :- N1 = N+1, biggerPrime(N1,P). primeListAcc(0,A,R,R) :- !. primeList(N,L) :- primeListAcc(N,1,[],L). primeListAcc(N,A,L,R) :- N1 is N-1, biggerPrime(A,P), A1 is P+1, primeListAcc(N1,A1,[P|L],R). And it works fine if I want the list ordered backwards: ?- primeList(5,L). L = [11, 7, 5, 3, 2]. But if I change the last line of the code from [P|L] to [L|P] like this:

Open list and member

别等时光非礼了梦想. 提交于 2019-12-11 01:45:49
问题 Since I want to avoid cost of append/3 , I use difference/open lists. The problem with an open list however is that member/2 reacts with an open list by adding the element to the tail. For example: ?- L=[a|_],member(b,L). L = [a, b|_G1196] ; L = [a, _G1195, b|_G1199] ; L = [a, _G1195, _G1198, b|_G1202] ; L = [a, _G1195, _G1198, _G1201, b|_G1205] ; L = [a, _G1195, _G1198, _G1201, _G1204, b|_G1208] ; L = [a, _G1195, _G1198, _G1201, _G1204, _G1207, b|_G1211] This is correct behavior since an

Is it possible to write an empty list as a difference list in Prolog?

冷暖自知 提交于 2019-12-10 20:49:33
问题 Empty lists are ... strange, to a Prolog beginner like myself. I would say that it isn't possible to write an empty list [] as a difference list T1-T2 just as it isn't possible to write an atom as a difference list. However, I would guess that to use recursion, there must be a way to use [] in a difference list setting. I have Google'd for this but I cannot find an answer, and Bratko (Prolog Programming for AI) only briefly touches the subject. So, is it possible to write an empty list as a

Structure (Difference Lists) Prolog

前提是你 提交于 2019-12-10 15:02:01
问题 This question refers to the material in chapter 3 of the book: Programming in Prolog, Clocksin and Mellish, Ed 5 In page 72 of this book, a program using difference list is displayed: partsOf(X,P):- partsacc(X,P,Hole) , Hole=[]. partsacc(X,[X|Hole],Hole):-basicpart(X). partsacc(X,P,Hole):- assembly(X,Subparts), partsacclist(Subparts, P, Hole). partsacclist([],Hole,Hole). partsacclist([P|T], Total, Hole):- partsacc(P,Total,Hole1), partsacclist(T,Hole1,Hole). In many tutorials online, the

Difference between “open-ended lists” and “difference lists”

試著忘記壹切 提交于 2019-12-08 19:03:25
问题 What is the difference between "open-ended lists" and "difference lists"? 回答1: Both notions seem to be lists, but in fact they are not. One is a concrete term, the other rather a convention. Open-ended lists, partial lists Open-ended lists are terms that are not lists but can be instantiated such that they become lists. In standard lingo, they are called partial lists . Here are partial lists: X , [a|X] , [X|X] are all partial lists. The notion open-ended lists suggests a certain usage of

How do you append an element to a list in place in Prolog?

ぃ、小莉子 提交于 2019-12-05 11:18:51
问题 If I have a list in Prolog such as X = [1, 2, 3, 4], how do I add the element 5 to the end of the list to have X = [1, 2, 3, 4, 5]? The append function needs two lists, ie append(A,B,C) to get A and B concatenated to the list C. I can do this with a temporary list Y = [1, 2, 3, 4] and Z = [5], to then do an append(Y, Z, X), but I don't like having a temporary list. The usual disclaimers apply here - this is not homework and I am just learning Prolog. 回答1: Variables in Prolog can only be