Intersection and union of 2 lists

Also, not sure why you’re dead against cuts, so long as their removal would not change the declaritive meaning of the code, as per your link. For example: inter([], _, []). inter([H1|T1], L2, [H1|Res]) :- member(H1, L2), inter(T1, L2, Res). inter([_|T1], L2, Res) :- inter(T1, L2, Res). test(X):- inter([1,3,5,2,4], [6,1,2], X), !. test(X). X = … Read more

Using \==/2 or dif/2

For elegance and didactic reasons alone, dif/2 is clearly preferable here and also in the vast majority of other cases, since as you already note “a lot of unnecessary unifications might take place” otherwise, and also because dif/2 is a pure and nicely declarative predicate that can be used in all directions and at any … Read more

How to count number of element occurrences in a list in Prolog

The following is based on my previous answer to “Remove duplicates in list (Prolog)” and on this previous answer to the question “Prolog union for A U B U C“. list_item_subtracted_count0_count/5 is derived from list_item_subtracted/3. list_counts/2 is derived from list_setB/2, which were both defined here. list_item_subtracted_count0_count([], _, [], N,N). list_item_subtracted_count0_count([A|As], E, Bs1, N0,N) :- if_(A … Read more

reversible “binary to number” predicate

Use CLP(FD) constraints, for example: :- use_module(library(clpfd)). binary_number(Bs0, N) :- reverse(Bs0, Bs), foldl(binary_number_, Bs, 0-0, _-N). binary_number_(B, I0-N0, I-N) :- B in 0..1, N #= N0 + B*2^I0, I #= I0 + 1. Example queries: ?- binary_number([1,0,1], N). N = 5. ?- binary_number(Bs, 5). Bs = [1, 0, 1] . ?- binary_number(Bs, N). Bs = … Read more

Splitting a list of integers into a list of positive integers and a list of negative integers

The recursive part is not quite correct. split([], [], []). split([Head|Tail], [Head|List1], List2) :- Head>=0, split(Tail, List1, List2). split([Head|Tail], List1, [Head|List2]) :- Head<0, split(Tail, List1, List2). The Head should be added to the positive list if Head >= 0 and to the negative list when Head < 0. Moreover, checking the sign of Head at … Read more

Shuffle in prolog

shuffle([], B, B). shuffle([H|A], B, [H|S]) :- shuffle(B, A, S). In this kind of problems, usually the difficult part is not Prolog but identifying the simplest recursive relation that solves it.

Integrating Prolog with C# [closed]

You can take a look at Yield Prolog. Yield Prolog uses yield keyword in C# (and Python, and JavaScript) and custom Variable class to simulate Prolog machine. This way, you get a Prolog API in your favourite language. You don’t need to connect your main language with P# or similiar projects.