Part of INTERACTIVE PROLOG GUIDE | Prev | TOC | Next |
This lesson covers data structures in Prolog. The basic data structure in Prolog is term which is expressed in form name(arguments...). If the number of arguments is zero then we speak about atom. A special type of atom is number.
In this section, we introduce a data structure date(Year,Month,Day) that represents date. First, we need a "constructor" of data structure date that makes the data structure from year, month and day:
Second, we define functions to access components of data structure in a following way:
get_xxx can be used to test or generate correspondent component of data structure, but it can not be used to set the value of the component. So, we have to define set_xxx to set values of components of data structure date.
Now, it is easy to find the "same" day in next or previous year respectively using get and set functions.
Note, that following definition of prev_year using next_year is not correct. Do you know why?
Finding next year is relatively easy but what about finding next day, i.e., tomorrow? Study following program to find where the possible problems are hidden. The definition of test correct_day follows the next section that covers working with lists.
Note that it is also possible and probably more reasonable to encapsulate the test correct_date to definitions of make_date and set_xxx.
List is a widely used data structure which is build in Prolog. It is still a term, e.g., [1,2,3] is equivalent to '.'(1,'.'(2,'.'(3,nil))). The following functions enable access to list elements.
It is easy to access the first element of list as it corresponds to the head. However, finding the last element is a time consuming process as one has to go through the whole list to find it. Note that following "procedures" can be used to find the first/last element of list as well as to test whether given element is first/last element of list. It could even be used to generate list with given first/last element.
The similar conclusion also holds for finding prefix and suffix respectively. Again, the same procedure can be used to test or generate prefix/suffix respectively as well as to generate list with given prefix/suffix. Try it.
Testing membership is an important method for working with lists. Prolog definiton of member can test membership relation as well as generate successive members of list. A similar function, nth_member, can also be used to test or to generate n-th member of list. However, it can not be used to count a sequence number of given element (define the function that counts a sequence number of given element as your homework).
Another popular function on lists is append which appends a list to another list. It can be also used to disjoint list (see following definition of prefix and suffix).
Now, prefix and suffix relations can be easily redefined using append:
append can be successfuly used in many other operations with lists including testing (or generating) sublist. The following rule exploits again the declarative character of Prolog.
There are (at least) two other ways how to define sublist, e.g., using prefix and suffix relations. All these definitions are equivalent. However, the procedure sublist3 is probably the closest to traditional (non-declarative) programming style as it uses technique known as "floating window".
Let us return to our example of data structure date. Now, we can define the test correct_date using lists.
First, we add two facts to Prolog database with distribution of days:
Then, we prepare test of intercalary years:
Finally, it is possible to test correctness of date:
Note, that the above definition of correct_day assumes that the only difference between regular and intercalary years is the number of days in February which is higher in intercalary years.
Part of INTERACTIVE PROLOG GUIDE | Prev | TOC | Next |