Pyke logic programming (5): Pattern Matching: "pattern variables"

Source: Internet
Author: User
Mode Variable Further, you may ask, "Who is the son of Thomas Norma ?". At this time, you input Thomas and Norma into Pyke and ask for more answers, not just "right" and "wrong ".
Mode variables are used as output variables. The format starts with the dollar sign $: family. son_of ($ son, Thomas, Norma)

You can use any name after the symbol $. Pyke correctly binds $ son to Bruce. If you are not satisfied with the answer, you can reject it and ask Pyke to give a new answer ("no, please continue searching !"). If Pyke finds out that the son of Thomas and Norma is correct, Pyke acknowledges that you are correct and binds $ son to another value.

If Pyke tells you that he is the last son of Thomas and Norma, And you reject the answer, Pyke will say to you, "You are wrong ".

We told Pyke, "No, please continue searching !" Then, Pyke will first free the constraint value of the pattern variable $ son, and then look for a new constraint value.

The value of the pattern variable at any time, or restricted or freed. If we track the pattern variables, we will see that the pattern variables will be constrained and freed in a moment when Pyke tries to find the appropriate answer. Once the search fails, $ son is relieved.

Anonymous Mode Variable

Suppose we want to know who is the son of Norma, but don't care about his father. Then, replace the father with an anonymous variable.

Anonymous variables can be of any type. The name of an anonymous variable. It starts with an underscore (_). Other characters do not matter. At most, they are helpful for document writing. But the symbol $ is indispensable.

So, "who is the son of Norma ?" In this case, Pyke indicates that:

Family. son_of ($ son, $ _ father, Norma)

The Norma parameter here is input from Pyke, $ son is output from Pyke, and $ _ father is used to tell Pyke who is a father is negligible.

An anonymous variable that never limits the value. Or, never assign a value to it. It is always free.

Normalization of pattern Variables

Please note! This issue is very important. The same pattern variable with the same name. For example, the two $ son you wrote are the same variable. A pattern variable can only be bound to one value at a time. Two identical values appear at the same time. variables with the same name must be written in two places.

Note that anonymous variables do not apply to the same operation because they do not constrain values, have no values, and can match any data values.

If you want to know how many sons have the same name as your father, you should ask:

Family. son_of ($ father, $ father, $ _ mother)

Note: The family_relations routine does not contain the same name as the parent and child, because it will not tell who is the Father, Son, and daughter, resulting in confusion.

Therefore, the Son name in the routine is unique.

If you ask such a question in a routine, the result will always fail.


Let's talk about how pattern variables match data values.

First, check whether the mode variable has bound the value.

If the value has been bound, it is compared with the data value. If it is consistent, the match is successful.

If no constraint value exists, the data value is matched with it, and the matching will always succeed.

Mode variables can match any value when they are not bound. After matching, the variable restricts that value. This is also a side effect of matching. After a pattern variable limits a value, it can only match exactly the same value, much like a text pattern.


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: 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.