prolog

Prolog Quicksort using second element as a pivot

ⅰ亾dé卋堺 提交于 2019-12-20 03:52:30
问题 I've been trying to learn prolog and I want to use the second element of a list as the pivot of a quicksort. I thought using [Head | [Pivot | Tail] ] as the input in the method would work, but then I was not sure where I could place "Head", the first element. like this: qsort([],[]):- !. qsort([Head|[Pivot|Tail]],Sorted):- split(Pivot,[Head|Tail],Less,Greater), qsort(Less,SortedLess), qsort(Greater,SortedGreater), append(SortedLess,[Pivot|SortedGreater],Sorted). split(_,[],[],[]). split(Pivot

count successive occurrences of number in Prolog

て烟熏妆下的殇ゞ 提交于 2019-12-20 03:43:26
问题 Hello I am trying to make a program in Prolog that given a list it counts the occurrences of each successive element in the list as follows: count(1,[1,1,1,2,2,2,3,1,1],0,X) the result would be X=[ [1,3],[2,3],[3,1][1,2] ] aka each sublist is [element,occurrences] In my case i believe there is something wrong with the base case but I cannot solve it. Can you help me? %append an element to a list append([ ],Y,Y). append([X|Xs],Ys,[X|Zs]):-append(Xs,Ys,Zs). %c is the counter beginning with 0

count successive occurrences of number in Prolog

烂漫一生 提交于 2019-12-20 03:43:09
问题 Hello I am trying to make a program in Prolog that given a list it counts the occurrences of each successive element in the list as follows: count(1,[1,1,1,2,2,2,3,1,1],0,X) the result would be X=[ [1,3],[2,3],[3,1][1,2] ] aka each sublist is [element,occurrences] In my case i believe there is something wrong with the base case but I cannot solve it. Can you help me? %append an element to a list append([ ],Y,Y). append([X|Xs],Ys,[X|Zs]):-append(Xs,Ys,Zs). %c is the counter beginning with 0

SWI-Prolog: splitting text atom into list of characters

♀尐吖头ヾ 提交于 2019-12-20 03:00:35
问题 Very simple question: I know there are plenty of ways to split an atom, eg 'example atom' on some delimiter, eg ' ' -> ['example', 'atom'] but is there a way to split up every character? Eg ['e', 'x', 'a', ... 'o', 'm']. I've tried atomic_list_concat(List, '', Atom), but that generates the error ERROR: atomic_list_concat/3: Domain error: `non_empty_atom' expected, found `' What would you recommend? 回答1: Do you want something like this? http://www.swi-prolog.org/pldoc/doc_for?object=atom_chars

How to multiply all elements of two lists with each other in Prolog

北城余情 提交于 2019-12-20 02:59:21
问题 I am thinking how to multiply all elements of two list with each other. Then I want to put all results in List3 . For example, List1 = [1,3,5]. List2 = [2,6,7]. List3 should contain [1x2, 1x6, 1x7, 3x2, 3x6, 3x7, 5x2, 5x6, 5x7]. In the end; List3 = [2, 6, 7, 6, 18, 21, 10, 30, 35]. Is it possible to do that? How to do that? I couldn't find a right way. 回答1: Well,First take a look on this question executing operation for each list element in swi-prolog and others to know how to do for-each

Term expansion for a list of terms

妖精的绣舞 提交于 2019-12-20 02:34:41
问题 Say I want to have a number of rules that all follow the same pattern. I have ran into this situation when I want to avoid non-deterministic behavior by explicitly listing all possible first arguments. I know, however, that I need to do exactly the same for some of the possibilities. One way to deal with it would be to have a catch-all clause at the end: foo(a) :- /* do something */. foo(b) :- /* do something else*/. foo(_). /* ignore the rest */ but this is not very nice because I can't

How is a integer created as a character code constant?

蹲街弑〆低调 提交于 2019-12-20 02:28:37
问题 I am building some test cases for integer/1 using SWI-Prolog. ISO/IEC 13211-1 gives a BNF definition for integer and one of the alternatives for integer is for a character code constant . I am able to create and test examples of all the other alternatives using integer/1 but for character code constant I cannot create a valid example. (see below) How is an integer as a character code constant created that will return true using integer/1? Answer Thanks to @false. integer(0'0). true. integer(0

Given values x and y return rule name if it is true

北城余情 提交于 2019-12-20 02:24:29
问题 This is my prolog file. male(bob). male(john). female(betty). female(dana). father(bob, john). father(bob, dana). mother(betty, john). mother(betty, dana). husband(X, Y) :- male(X), mother(Y, Z), father(X, Z). wife(X, Y) :- female(X), father(Y, Z), mother(X, Z). son(X, Y) :- male(X), mother(Y, X);female(X), father(Y, X). daughter(X, Y) :- female(X), mother(Y, X);female(X), father(Y, X). sister(X, Y) :- female(X), mother(Z, X), mother(Z, Y), X \= Y. brother(X, Y) :- male(X), mother(Z, X),

Very basic dcg prolog syntax

Deadly 提交于 2019-12-20 01:45:15
问题 I am trying to understand prolog and definite clause grammar but I am having a very hard time understanding both of them. I am really trying to understand how to use the dcg syntax... Here I have two examples: The first is actually the code from another question on this forum but with an additional question: The code looked like this: s --> first, operator, second. first --> [X]. operator --> ['+']. second --> [X]. And when Prolog is asked about this, it returns true/false but I can't for the

Prolog delete: doesn't delete all elements that unify with Element

纵饮孤独 提交于 2019-12-20 01:39:55
问题 I'm having an issue with SWI-Prolog's delete/3 predicate. The easiest way is just a quick example: ?- delete([(1,1),(1,2),(3,2)], (1,_), List). List = [(1,2),(3,2)]. I would expect (1,2) to also be deleted, since (1,_) unifies with (1,2) . The SWIPL help says: Delete all members of List1 that simultaneously unify with Elem and unify the result with List2 . Why is this and how can I delete everything that unifies with (1,_) ? 回答1: " Delete all members of List1 that simultaneously unify with