prolog

Subset function in prolog

做~自己de王妃 提交于 2020-01-05 08:44:50
问题 I am working on writing a subset function and I have succeeded in doing so. Here's my function which implements member function: member( X, [ X | T ] ). member( X, [ _ | T ] ) :- member( X, T ). subset([], _). subset([H|T1], T2) :- member(H, T2), subset(T1, T2). subset([H1|T1], [H2|T2]) :- \+ member(H1, T2), subset([H1|T1], T2). My question is, is there a better way to write this function using the member function of course. 回答1: The third clause of subset/3 does not make sense and i think

How to remove the repeated members in a simple list [ prolog ]

梦想的初衷 提交于 2020-01-05 08:27:44
问题 EDIT : How can I remove the repeated members in a simple list for example : [a,b,b,b,c,c,e] in this list the are 2 c and 3 b and I want to remove all members that's repeated the result should be like this [a,e] keep in mind I'm just learning the basic just for an assignment and I'm using the swish online compiler 回答1: I have edited my previous code. My previous code gives the output in reverse order. I used cut here so that it wouldn't backtrack all the possibilities of takeout function. Hope

How to remove the repeated members in a simple list [ prolog ]

两盒软妹~` 提交于 2020-01-05 08:27:37
问题 EDIT : How can I remove the repeated members in a simple list for example : [a,b,b,b,c,c,e] in this list the are 2 c and 3 b and I want to remove all members that's repeated the result should be like this [a,e] keep in mind I'm just learning the basic just for an assignment and I'm using the swish online compiler 回答1: I have edited my previous code. My previous code gives the output in reverse order. I used cut here so that it wouldn't backtrack all the possibilities of takeout function. Hope

how do I interact with the inferior prolog process in emacs?

吃可爱长大的小学妹 提交于 2020-01-05 07:56:27
问题 I've installed SWI-Prolog and emacs' prolog mode. I have a first_steps.pl file with prolog in it, and I do C-c C-b to consult the buffer. This opens a second buffer, prolog , whose mode is (Inferior Prolog: run). All well and good. However, it's completely unclear how to interact with that inferior prolog mode buffer. There is no prompt. Typing things and pressing return does nothing. How can I execute queries based on the code consulted from the buffer, i.e, interact with SWI-prolog? Note

Variables and how they are set and used in prolog

只谈情不闲聊 提交于 2020-01-05 07:54:30
问题 http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_1.html So on that tutorial where it has: conflict(Coloring) :- adjacent(X,Y), color(X,Color,Coloring), color(Y,Color,Coloring). Am I understanding this correctly, that Color is a variable and is set to a value after the first call to color and then that value is used in the second call to color ? 回答1: Variables in Prolog: All variables and arguments are local in scope to the predicate in which they are declared (aka first used).

Why am I getting an existence error on predsort?

安稳与你 提交于 2020-01-05 07:37:17
问题 I am trying to sort a list of paths by the distance it takes to complete them. The Prolog code I'm using is below. When I call sortRoutes , I get an existence error from Prolog saying that predsort doesn't exist. However, I am using the sort module and that doesn't seem to change anything. I can't seem to figure out why this isn't working. Am I doing anything wrong? Thanks! :- use_module(library(sort)). sortRoutes(DistRoutes, SortedRoutes) :- predsort(distCompare, DistRoutes, SortedRoutes).

Why am I getting an existence error on predsort?

泄露秘密 提交于 2020-01-05 07:35:02
问题 I am trying to sort a list of paths by the distance it takes to complete them. The Prolog code I'm using is below. When I call sortRoutes , I get an existence error from Prolog saying that predsort doesn't exist. However, I am using the sort module and that doesn't seem to change anything. I can't seem to figure out why this isn't working. Am I doing anything wrong? Thanks! :- use_module(library(sort)). sortRoutes(DistRoutes, SortedRoutes) :- predsort(distCompare, DistRoutes, SortedRoutes).

Double elements in list using prolog?

≡放荡痞女 提交于 2020-01-05 07:11:11
问题 How can I double even numbers in a list in Prolog? For example: X=[1,2,3,5,4] The result should be: X=[1,2,2,3,5,4,4] Thank you! 回答1: Even check can probably be done better, but it kinda works. even(N) :- N mod 2 =:= 0. doubleeven([],[]). doubleeven([H|T], [H,H|Z]) :- even(H), !, doubleeven(T,Z). doubleeven([H|T], [H|Z]) :- doubleeven(T,Z). 回答2: Based on iwhen/2, first define the reified test predicate eveninteger_t/2 : eveninteger_t(I, T) :- iwhen(nonvar(I), ( 0 is I mod 2 -> T = true ; T =

Double elements in list using prolog?

北战南征 提交于 2020-01-05 07:11:11
问题 How can I double even numbers in a list in Prolog? For example: X=[1,2,3,5,4] The result should be: X=[1,2,2,3,5,4,4] Thank you! 回答1: Even check can probably be done better, but it kinda works. even(N) :- N mod 2 =:= 0. doubleeven([],[]). doubleeven([H|T], [H,H|Z]) :- even(H), !, doubleeven(T,Z). doubleeven([H|T], [H|Z]) :- doubleeven(T,Z). 回答2: Based on iwhen/2, first define the reified test predicate eveninteger_t/2 : eveninteger_t(I, T) :- iwhen(nonvar(I), ( 0 is I mod 2 -> T = true ; T =

Iteration over relations using a database

假如想象 提交于 2020-01-05 06:56:31
问题 I'm new to prolog. Consider the following format: carsCompany(Tel_Number, Manager, Company_Name, [new_cars(Car_Name,info(Color,Creator),Date_Creation)], [old_cars(Car_Name,info(Color,Creator),Date_Creation)] ]). I would like to create a two argument relation which gets a list of car names and a company and adds them to the company. update_company([Mazda],Company). I have the following db example: carsCompany(1234, Jujiro Matsuda, Mazda, [new_cars(mazda_3,info(Grey,Person1),26082016)], [old