After studying the contents of the first three chapters, we should have an intuitive and rational understanding of Prolog programming. Since unity, variable initialization, proving search and recursion are all core concepts of Prolog, there are a few more practices and exercises.
Here I will first enter the topic, and later give my own program code and some thinking.
Practice 1
Imagine a knowledge base that describes the maze. The fact that it describes the relationship between points and points, that is, the CONNECTED/2 predicate logic gives the fact that the maze can be reached from the point of the parameter 1, directly to the point of the parameter 2. And
Unicom relationship is a direction, one-way cannot return:
Connected1,2). Connected (3,4). Connected (5,6). Connected (7,8). Connected (9,Ten). Connected ( A, -). Connected ( -, -). Connected ( the, -). Connected ( -, -). Connected ( +, -). Connected (4,1). Connected (6,3). Connected (4,7). Connected (6, One). Connected ( -,9). Connected ( One, the). Connected ( -, A). Connected ( -, -). Connected ( -, +).
Please write out a predicate logic PATH/2 which points in the maze can be connected (directly or indirectly). For example, can point 5 reach point 10? What points can I reach when the starting point is 1 o'clock?
Practice 2
The following traffic information Knowledge Base:
Bycar (Auckland, Hamilton). Bycar (Hamilton, Raglan) Bycar (Valmont, Saarbruecken). Bycar (Valmont, Metz). Bytrain (Metz, FRANKFURT). Bytrain (Saarbruecken, Frankfurt). Bytrain (Metz, Paris). Bytrain (Saarbruecken, Bangkok). Byplane (Frankfurt, Singapore) Byplane (Paris, LosAngeles). Byplane (Bangkok, Auckland). Byplane (Singapore, Auckland). Byplane (LosAngeles, Auckland).
write a predicate logic TRAVEL/2 that can be described from one location to another (indirect or direct) and can be transferred by car, train, and plane. For example, if the query:
?-Travel (Valmont, Raglan).
Prolog will answer true.
Practice 3
So, by practicing the predicate logic TRAVEL/2 in 2, we can know that Valmont can reach Raglan. This is useful, but when you're planning a trip, you want more precise information, such as the detailed path
How about that. Please write a predicate logic TRAVEL/3 that can tell the specific path, for example, if the query:
?-Travel (Valmont, LosAngeles, X).
X = Go (Valmont, Metz, Go (Metz, Paris, Go (Paris, LosAngeles)))
Practice 4
Extend the predicate logic TRAVEL/3 in practice 3 so that it can not only give a route from one place to another, but also give a way to get there. That is, the new program can let us know that in every segment of travel, is
The use of car,train or plane as traffic work.
In the future study, I will find more practical topics, including the famous Sudoku problem, four-color map problem, eight queen problem, Hanoi Tower problem, and so on, one by one analysis. The answers to this chapter's practice will be attached in succession.
Learn Prolog now Translations-Chapter III-recursion-fourth, more practice and practice