Learn Prolog now Translations-fourth chapter-List-first section, list definition and use

Source: Internet
Author: User

Content Summary

List definition;

The use of oneness in the list;

anonymous variables;

List definition

As the name implies, a list is a collection of multiple elements. More precisely, it is a finite sequence of elements. In the Prolog list, there are some specific examples:

[Mia, Vincent, Jules, Yolanda]

[Mia, Robber (Honey_bunny), X, 2, Mia]

[ ]

[Mia, [Vincent, Jules], [Butch, Girlfriend (Butch)]

[[], Dead (z), [2, [B, C]], [], Z, [2, [B, C]]]

From the example above, we can learn the following important things:

1. In Prolog, enclose a list (i.e. "[]") with parentheses in the English character, and the elements in the list are separated by commas (i.e., ","); For example, the first list above, [Mia, Vincent, Jules, Yolanda],

is a list of 4 elements, namely, Mia,vincent,jules, and Yolanda; the length of a list is the number of elements it contains, so the first example has a list of 4.

2. From the second example, [Mia, Robber (Honey_bunny), X, 2, Mia], we can know that all types in Prolog can be elements of a list; the first element in the example is Mia, which is an atom; the second element is a

Complex statement; The third element is X, which is a variable, and the fourth element is 2, which is a number. Furthermore, we can further know that the same element may appear multiple times in a list, for example, the Fifth element is a polygon,

and Section an element is the same atom.

3. The third example shows a special list, an empty list. An empty list (as the name implies) is a list that does not contain any elements. What about the length of the empty list? Of course it's 0.

4. The fourth example reveals a very important thing: A list can contain other lists as its elements. For example, the second element in this example is: [Vincent, Jules], and the third element is: [Butch, Girlfriend (Butch)].

so What is the list length of the fourth example? The answer is 3. If you think it is 5 (or something else), then the way you think about the list may not be correct. The elements of the list are encapsulated in the outermost brackets, separated by commas.

so this The list has 3 elements: the first is MIA, the second is [Vincent, Jules], and the third one is [Butch, Girlfriend (Butch)].

5. The last example merges all the previous concepts together. So we have a list that contains an empty list (in fact, contains two), Complex statements: Dead (Z), two identical sub-lists: [2, [B, C]],

and a variable Z. Note that the third (and last) element is a list, and it also contains a sub-list (that is, [B, C]).

The next important point is that any non-empty list can be thought of as two parts: Head and tail (head and tail). The head is simply the first element of the list, and the tail is the rest of the list.

to more precisely definition, trailing refers to the list after the first element is removed from the list. That is, the tail of a list is still a list. For example, the following list:

[Mia, Vincent, Jules, Yolanda]

The head is MIA, and its tail is: [Vincent, Jules, Yolanda]. Similarly, the list:

[[], Dead (z), [2, [B, C]], [], Z, [2, [B, C]]]

The head is [], the Tail is [Dead (z), [2, [B, C]], [], Z, [2, [B, C]]]. So what is the head and tail of the list [Dead (z)]? Because the head is the first element of the list, it is Dead (z) and the tail is the first element removed

After the list, here, is the empty list: [].

What about the empty list? It has no head and tail. That is, the empty list has no internal structure; for Prolog, [] is a special, unique list. Later we will learn the fact that using recursive list operations, empty lists

Plays an important role in Prolog programming.

The use of Oneness in the list

Prolog has an important built-in operator | (a vertical line) that breaks down the list into its head and tail. It is important to know how to use | Because this symbol is the core tool for Prolog list operations.

| The most important role is to extract information from the list. We achieve this goal by using | and together. For example, in order to get the list: [Mia, Vincent, Jules, Yolanda] the head and tail, we can make the following query:

?-[Head | Tail] = [Mia, Vincent, Jules, Yolanda].

Head = Mia

Tail = [Vincent, Jules, Yolanda]

True

That is, the head of the list is already bound to the head, and the end of the list is bound to tail. Note that there is nothing special here to head and tail, they are simply variables. We can also use the following query:

?-[X | Y] = [Mia, Vincent, Jules, Yolanda].

X = Mia

Y = [Vincent, Jules, Yolanda]

True

As we mentioned before, only the non-empty list has head and tail, if we use | to parse [],prolog will answer false:

?-[X | Y] = [].

False

That is, Prolog treats an empty list as a special list, the result is very important and we will see later in the study.

Let's look at another example. We can extract the head and tail from below to the list, as we saw earlier:

?-[X | Y] = [[], Dead (z), [2, [B,c]], [], z].

X = []

Y = [Dead (z), [2, [B,c]], [], _7800]

Z = _7800

True

Explain: The head of the list and the x binding, the tail and the Y binding. (we can also see that z and an intermediate variable _7800 are bound)

But we can use multiple |, it's a very flexible big tool. For example, suppose we want to be able to get the first two elements of the list, and the rest of the list after the second element, we can do the following query:

?-[X, Y | W] = [[], Dead (z), [2, [B,c]], [], z].

X = []

Y = Dead (z)

W = [[2, [B,c]], [], _8327]

Z = _8327

True

That is, the head of the list is bound to x, the second element is bound to Y, and the remainder of the list is bound to W (W is also a list, that is, the original list is stripped of the first two elements). So | not just

The ability to divide the list into headers and tails, and to explode from any point list, the left side of | Indicates how many elements we want to advance from the front, and the right side of | Indicates the rest of the list we want.

Anonymous variables

It's time to introduce anonymous variables. Suppose we are interested in the second and fourth elements in the following list:

[[], Dead (z), [2, [B, C]], [], z].

We can use the following method to produce the result:

?-[X1, X2, X3, X4 | Tail] = [[], Dead (z), [2, [B, C]], [], z].

X1 = []

X2 = Dead (z)

X3 = [2, [B, C]]

X4 = []

Tail = [_8910]

Z = _8910

True

Yes, we've got the data we want, the information we're interested in has been bundled with x2,x4, but we've also gotten a lot of other information, like X1,X3

And tail, maybe that's not what we're interested in. And it is foolish to introduce three unrelated variables to deal with such requirements. In fact, we can do this by making the following inquiries up

to the target:

?-[_, X, _, Y | _] = [[], Dead (z), [2, [B,c]], z].

X = Dead (z)

Y = []

Z = _9523

True

The English character underline ("_") represents an anonymous variable. An anonymous variable is used when we need to use a variable, but also to care about what value the variable needs to be bound. As you're on top of this

As you can see in the example, _ is not bound to any value. Also, note that each _ is independent: Each represents a number of different variables. This condition is not allowed in normal variables, but

A name variable is not a normal variable, it is just a prolog used to require a variable in a particular position, and each is independent of the other variables.

Let's take a look at the last example. The third element of the list above is also a list (i.e. [2, [B,c]]), assuming we want to get the tail of this built-in list, and are not interested in any other information, I

The following queries can be made:

?- [_, _, [_, | X] | _] = [[], Dead (z), [2, [B,c]], z, [2, [B,c]]].

X = [[B,c]]

Z = _10087

True

Learn Prolog now Translations-fourth chapter-List-first section, list definition and use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.