Basic prolog program
That's because 1 was the menu option for running a query. And we're almost done! The last thing we need to do is define those predicates for printing. We want to print out the results of our query - true or false - so we'll define a predicate for each of them. The actual work isn't too bad.
Note that we're doing basically the same thing but to print the false results, we are checking for a negative evaluation of our query. And that's it.
So put all together our main. Swi-prolog allows you to compile using the same command that you use tostart up the interactive interpreter.
A full listing of the options is available online at swi-prolog. The ones that I use are: --goal: This is basically to help prolog determine the exit code of the program. I'm using the output from the main predicate. This also clues prolog in on what predicate to start with so it will run main. Without this, when your program runs you'll get a prolog prompt and you will have to type in main with is not what we want.
I haven't noticed any problems if I fail to include this, but I include it anyway. We will discuss about the differences between Logic programming and the traditional functional programming languages. From this illustration, we can see that in Functional Programming, we have to define the procedures, and the rule how the procedures work.
These procedures work step by step to solve one specific problem based on the algorithm. On the other hand, for the Logic Programming, we will provide knowledge base. Using this knowledge base, the machine can find answers to the given questions, which is totally different from functional programming.
In functional programming, we have to mention how one problem can be solved, but in logic programming we have to specify for which problem we actually want the solution.
Then the logic programming automatically finds a suitable solution that will help us solve that specific problem. It is one major example of the fourth generation language that supports the declarative programming paradigm.
This is particularly suitable for programs that involve symbolic or non-numeric computation. This is the main reason to use Prolog as the programming language in Artificial Intelligence , where symbol manipulation and inference manipulation are the fundamental tasks.
In Prolog, we need not mention the way how one problem can be solved, we just need to mention what the problem is, so that Prolog automatically solves it. However, in Prolog we are supposed to give clues as the solution method.
To satisfy a rule these conditions should be met. So if the set is [a,b], then the result will be [], [a], [b], [a,b]. It will take L and return each subsets into X. As you know if two lists have the same element twice, then after union, there will be only one.
So we need another helper clause to check the membership. Intersection will return those elements that are present in both lists. In this example, we will see two operations using which we can check whether the list has odd number of elements or the even number of elements. Otherwise we take it as [Head Tail], then if Tail is of odd length, then the total list is even length string.
This operation divides a list into two lists, and these lists are of approximately same length. So if the given list is [a,b,c,d,e], then the result will be [a,c,e],[b,d]. This will place all of the odd placed elements into one list, and all even placed elements into another list.
If there is only one element, then the first list will be a list with that element, and the second list will be empty. This operation is used to find the maximum element from a list. Divide the list as [X,Y Tail]. If the list is [4,5,3,7,8,1,2], then the result will be [1,2,3,4,5,7,8].
Arnab Chakraborty. Prolog - Lists Advertisements. Previous Page. Next Page. Useful Video Courses. More Detail. Previous Page Print Page.
0コメント