prolog

How to check which items on the list meet certain condition?

空扰寡人 提交于 2020-01-11 12:50:56
问题 How to make a function called busLineLonger, which receives at least two parameters to decide if a bus line is longer or not? */This is how it works*/ * busStops(number_of_the_bus,number_of_stops)*/ /*?- busLineLonger([busStops(1,7),busStops(2,4),busStops(3,6)],5,WHICH). * WHICH = [1,3]. Using only comparative things, like @> <@ /==@. Sorry my english Edit... So far I've think of something like this busLineLonger([busStops(A,B)|R],N,[_|_]):- N@>B, busLineLonger(R,N,A). 回答1: Here's how you

Program in Prolog for sum of numbers in interval

假如想象 提交于 2020-01-11 11:46:33
问题 I'm trying to create a program in Prolog which takes two numbers - A and B and finds the sum of the numbers from A to B, including them. To sum up: sum(1, 5, C) should find that C is 1+2+3+4+5 = 15 This is my code, which is not working :( sum(A, B, C) :- A =< B, A1 is A+1, C1 is C+A, sum(A1, B, C1). And then I test it with sum(1, 5, C). What I get is ERROR: is/2: Arguments are not sufficiently instantiated , but I can't really get what is the problem. :( Could you please help me to understand

Longest increasing subset Prolog

不羁岁月 提交于 2020-01-11 11:45:08
问题 I want to create in Prolog to find longest increasing subset of entered list. For example, you enter list of [3,1,2] and the output is [1,2], ?- subset([3,1,2], X). X = [1,2] I have code which shows all the subsets of this list: subset([],[]). subset([_|X],Y):-subset(X,Y). subset([A|X],[A|Y]):-subset(X,Y). Can anyone help me to find just the longest increasing subset? 回答1: Do you mean [1,3,5,6,7] to be the answer for [4,1,3,8,9,5,6,7] ? IOW, do you really mean subsets, or just sublists, i.e.

Postfix to Prefix Conversion using Prolog

孤者浪人 提交于 2020-01-11 11:26:28
问题 Can anyone help me to write a program using stack concept in PROLOG to convert an arithmetic expression from postfix(reverse polish notation) to prefix form. The arithmetic expression may contain the 4 arithmetic operators + , - , / , * and the unary functions : sin, cos, tan, exp, log and sqrt. 回答1: append/2 it's a useful list combinator. It allows in fairly general way a relation of concatenation among an arbitrary number of lists. I'll show just the basic here, you'll need to complete your

Postfix to Prefix Conversion using Prolog

ぐ巨炮叔叔 提交于 2020-01-11 11:26:16
问题 Can anyone help me to write a program using stack concept in PROLOG to convert an arithmetic expression from postfix(reverse polish notation) to prefix form. The arithmetic expression may contain the 4 arithmetic operators + , - , / , * and the unary functions : sin, cos, tan, exp, log and sqrt. 回答1: append/2 it's a useful list combinator. It allows in fairly general way a relation of concatenation among an arbitrary number of lists. I'll show just the basic here, you'll need to complete your

How to use prolog in java?

徘徊边缘 提交于 2020-01-11 09:29:06
问题 Please, someone help me with an example. I would like to create a simple Prolog program, that adds two numbers together, and the Java program writes it to the console! So the Prolog is adding, and the a Java program is writing the result. I can write the adding in prolog, but I don't know how to invoke the prolog program in java. =( Please, someone, who can create this little example, write it here! Thanks!! 回答1: You can try to use the the GNU Prolog for Java. Running an existing prolog file

how to create Meta-rules and/or meta-interpreter for an Expert System with Swi-Prolog

依然范特西╮ 提交于 2020-01-11 07:21:26
问题 I wanna create an expert system with meta-interpreter with SWI-Prolog... what is the best and the easier way to make it? which is the procedure to make it? 回答1: Many of the meta-interpreters for expert systems are based on the so called vanilla interpreter. This is an interpreter for Prolog without cut and without built-ins. it reads as follows: solve(true) :- !. solve((A,B)) :- !, solve(A), solve(B). solve(H) :- clause(H,B), solve(B). You can readily use it to solve the following knowledge

How do I append 3 lists efficiently in Prolog?

被刻印的时光 ゝ 提交于 2020-01-11 06:00:27
问题 I know how to do it for 2 lists: append([],L,L). append([H|T],L,[H|R]):-append(T,L,R). but how to do it for 3? Without using the append for 2 lists twice. 回答1: To append lists efficiently, consider using difference lists . A difference list is a list expressed using a term with two lists. The most common representation uses (-)/2 as the functor for the term. For example, the list [1,2,3] can be expressed as: [1,2,3| Tail]-Tail. By keeping track of the list tail, i.e. of its open end , you can

How do I append 3 lists efficiently in Prolog?

爱⌒轻易说出口 提交于 2020-01-11 06:00:15
问题 I know how to do it for 2 lists: append([],L,L). append([H|T],L,[H|R]):-append(T,L,R). but how to do it for 3? Without using the append for 2 lists twice. 回答1: To append lists efficiently, consider using difference lists . A difference list is a list expressed using a term with two lists. The most common representation uses (-)/2 as the functor for the term. For example, the list [1,2,3] can be expressed as: [1,2,3| Tail]-Tail. By keeping track of the list tail, i.e. of its open end , you can

How do I append 3 lists efficiently in Prolog?

风流意气都作罢 提交于 2020-01-11 05:59:04
问题 I know how to do it for 2 lists: append([],L,L). append([H|T],L,[H|R]):-append(T,L,R). but how to do it for 3? Without using the append for 2 lists twice. 回答1: To append lists efficiently, consider using difference lists . A difference list is a list expressed using a term with two lists. The most common representation uses (-)/2 as the functor for the term. For example, the list [1,2,3] can be expressed as: [1,2,3| Tail]-Tail. By keeping track of the list tail, i.e. of its open end , you can