prolog

Prolog: splitting a list into two lists (unique items / duplicate items)

偶尔善良 提交于 2019-12-20 01:36:31
问题 I have been trying to split a given list into two different lists: Unique and Duplicate. For example, if we have the list [1, 1, 2, 3, 3, 4, 5] I want the Unique list to be [2, 4, 5] and Duplicate to be [1, 3] . I don't want all the 1's in the list to be in the Duplicate list. I just need one of it. The code I have right now: compareL([_|[]], Unique, Dup). compareL([X3,Y3 | Tail], [X3 | Unique], Dup) :- X3 =\= Y3, compareL([Y3 | Tail], Unique, Dup). compareL([X3,Y3 | Tail], Unique, [X3 | Dup]

Finding Length of List in Prolog

霸气de小男生 提交于 2019-12-20 01:11:06
问题 I'm running Prolog and trying to write a small function returning the length of a list: len([],0). len([XS], Y) :- len([X|XS], M), Y is M+1. My logic is that the recursive call should include the tail of the list (XS) and increase 1 to the previous length (Y is M+1.) This always returns false. Any thoughts? 回答1: Here is a general methodology for debugging and testing Prolog predicates: Start with the most general query! Think of it: In Prolog you do not need to make up some test data. You don

Uneven tabling performance in BProlog 8.1

…衆ロ難τιáo~ 提交于 2019-12-19 19:50:06
问题 I did a few experiments with the tabling capabilities of b-prolog version 8.1 and was quite surprised by the performance I observed. Here is the code that I used. It counts the number of Collatz steps N required for reducing some positive integer I down to 1 : %:- table posInt_CollatzSteps/2. % remove comment to enable tabling posInt_CollatzSteps(I,N) :- ( I == 1 -> N = 0 % base case ; 1 is I /\ 1 -> I0 is I*3+1, posInt_CollatzSteps(I0,N0), N is N0+1 % odd ; I0 is I>>1, posInt_CollatzSteps(I0

Predsort/3 like msort/2

纵饮孤独 提交于 2019-12-19 18:28:14
问题 I would like to know is it possible to use predsort/3 without losing duplicate values? If not, that how should I sort this list of terms? Current sort function: compareSecond(Delta, n(_, A, _), n(_, B, _)):- compare(Delta, A, B). Result: predsort(compareSecond, [n(3, 1, 5), n(0, 0, 0), n(8, 0, 9)], X). X = [n(0, 0, 0), n(3, 1, 5)]. You see, that term n(8,0,9) is gone and that's not what I need. 回答1: predsort will remove duplicates, but it leaves it to the comparison predicate to define which

Convert peano number s(N) to integer in Prolog

邮差的信 提交于 2019-12-19 17:47:13
问题 I came across this natural number evaluation of logical numbers in a tutorial and it's been giving me some headache: natural_number(0). natural_number(s(N)) :- natural_number(N). The rule roughly states that: if N is 0 it's natural, if not we try to send the contents of s/1 back recursively to the rule until the content is 0 , then it's a natural number if not then it's not. So I tested the above logic implementation, thought to myself, well this works if I want to represent s(0) as 1 and s(s

Prolog - List of CharCodes to a String or Characters

风流意气都作罢 提交于 2019-12-19 17:45:39
问题 I have a list of Character Codes in prolog. I would like to change them into characters. For instance, L = "abc" returns L = [97,98,99] Assuming I start with L = [97,98,99] Is there anyway to convert L back into abc such that, if there exists a method convert(L, X) returns X = abc Thanks. 回答1: Given L="abc", convert(L, X), X = abc I'd say that you want to get atom (see Data types description) from prolog string. I guess you want atom_codes/2 or something like that. It should work like L="abc"

Prolog: predicate for maximum without accumulator

和自甴很熟 提交于 2019-12-19 17:33:11
问题 Is it possible to create a predicate max/2 without an accumulator so that max(List, Max) is true if and only if Max is the maximum value of List (a list of integers)? 回答1: Yes, you can calculate the maximum after the recursive step. Like: max([M],M). % the maximum of a list with one element is that element. max([H|T],M) :- max(T,M1), % first calculate the maximum of the tail. M is max(H,M1). % then calculate the real maximum as the max of % head an the maximum of the tail. This predicate will

Hilog terms in (XSB) Prolog

那年仲夏 提交于 2019-12-19 16:19:30
问题 Are Hilog terms (i.e. compounds having as functors arbitrary terms) still regarded as a powerful feature in XSB Prolog (or any other Prolog) ? Are there many XSB projects currently using this feature ? which of them for example ? I ask since as far as I understand higher order programming is equally possible using the ISO built-in call/N. Specifically, I would like to understand if XSB is using Hilog terms just for historical reasons or if Hilog terms have considerable advantages in

Prolog - Finding adjacent elements in a list

喜你入骨 提交于 2019-12-19 12:24:55
问题 I'm trying to define a predicate adjacent(X, Y, Zs) that is true if X and Y are adjacent in a list. My code is currently this: adjacent(_, _, []). adjacent(X, Y, [X, Y|Tail]) :- adjacent(X,Y, Tail). It works for the basic case of adjacent(c, d, [a, b, c, d, e]) , but due to the base case, every other case returns true as well, and I'm stuck on that. The other problem is that if X is not equal to the first part of the list's head, then it skips past both X and Y and goes to the next 'X'; e.g.,

Prolog - Finding adjacent elements in a list

不羁的心 提交于 2019-12-19 12:24:02
问题 I'm trying to define a predicate adjacent(X, Y, Zs) that is true if X and Y are adjacent in a list. My code is currently this: adjacent(_, _, []). adjacent(X, Y, [X, Y|Tail]) :- adjacent(X,Y, Tail). It works for the basic case of adjacent(c, d, [a, b, c, d, e]) , but due to the base case, every other case returns true as well, and I'm stuck on that. The other problem is that if X is not equal to the first part of the list's head, then it skips past both X and Y and goes to the next 'X'; e.g.,