See Martial Arts Programming _ nine Gongge as an example introduce powerful declarative language prolog

Source: Internet
Author: User

If you want to divide a number of programming languages into classes, you might divide them into low-level and high-level languages, or into object-oriented languages and process-oriented languages. However, more Chinese programmers are unfamiliar with another way of dividing the computer language into a command-type and declarative two camps. The reason why we may not be familiar with this division is that most of the languages we use are usually imperative. But in fact you should also notice the existence of another big camp.

Imperative programming (imperativeprogramming) is the most widely used programming paradigm today. Many of the computer languages familiar to readers, such as C, C + +, Java, Pascal, Basic, Python, JavaScript, etc., all belong to the category of imperative programming languages.

Contrary to imperative programming, declarative programming (declarative programming). Declarative programming differs greatly from imperative programming. Imperative programming is to command the machine how to do things, so whatever you want, it will follow your command. declarative programming is telling the machine what you want and letting the machine figure out how to do it. ( this is a very difficult thing to imagine for programmers who are used to the imperative language, but we'll give an example to illustrate this "smart") declarative programming to describe the nature of the object, so that the computer understands the goal, not the process. Declarative programming does not need to tell the computer the problem area to avoid the attendant side effects. Instruction programming requires an algorithm to clearly indicate how each step should be done.

Declarative programming languages are represented by Prolog, Haskell, Erlang, Lisp, and so on (in fact, there is a kind of SQL language that you might be more familiar with). We can also divide these languages into two categories-functional programming languages and logical programming languages. Readers familiar with the history of the computer should know that Turing uses a Turing machine to define the algorithm, while Church uses lambda calculus to define the algorithm, and these two definitions are equivalent (this is also known as the Church-Turing thesis). Imperative programming is extended from the concept of Turing, and functional programming is based on the concept of lambda calculus and functions. The current more popular functional programming language represents non-Haskell genus.


Turing and church

Another big faction in declarative programming languages is the logical programming language, in which a very important representation is the Prolog language. Logical programming is based on the concept of predicate calculus and relationship proposed by German mathematician and Gottlob Fregg (Gottlob Frege). A function is a special relationship that represents a one-way relationship from input to output, and when input is fixed, the output is uniquely determined. Relationships do not have these limitations. From this perspective, logical programming actually covers functional programming.

" If a computer language can't affect your way of thinking, it's not worth learning, " said Alan Perlis, a computer scientist at the Allen Pelly, who was the first Turing Award winner. Prolog may be a language worthy of your study, as it will completely subvert your existing understanding of programming.

First of all, Prolog is not an emerging language, but instead it is a very old language. As the first logical programming language to emerge in history, Prolog was originally developed by Alain Colmeraure of the University of Aix-Marseille (Alain Colmerauer) and others in the late 70.

Second, Prolog is not a very advanced language, and conversely, Prolog is a much easier-to-understand language than other programming languages such as C + +, Python, and so on. If you have never been in touch with computer programming, then congratulations, you will easily enter the Prolog world. If you are already a master of other imperative languages, you need to completely discard your original programming ideas, otherwise it is difficult to master Prolog.

This makes me think of Jin Yong's novel in the old urchin Botong Tong self-creation of the right and left . It is said that Zhou Botong was a yellow pharmacist imprisoned in the Peach Blossom Island, because bored, so ingenuity thought left-handed and right-handed fights, to self-joy. So he created this strange science called Right and left. Later, by chance coincidence, wounded Zhou Botong and Little Dragon woman was gold round the king forced into the desperate. Small Longnu and Yang once double swords, defeated Gold round King. And when Yang was not around, the little Dragon Female Tango, Zhou Botong learned after that will be left and right to each other in the Little Dragon female. But Zhou Botong also frankly said, Huang Rong very smart, but always can not have to right and left to the law, but Guo Jing qualification mediocre, but can learn. And this other one learned about the inner peace is the Dragon Girl. The end of the Little Dragon Woman one of the two corners, the herself sword, the success of the Golden Wheel defeated King.

Having said so much, at last we illustrate how Prolog can accomplish tasks intelligently by solving nine Gongge as an example. There is such a bridge in the martial arts heroic biography of the hero. The Hermit Biei all day in the bitter repair strange door, do not want to be a computation trapped, although hundred think but not its solution. The problem was eventually easily cracked by the Huang Rong of the gifted. Nine Gongge is an ancient mathematical game, which requires that in a matrix of three longitudinal three horizontal, fill in from 1 to 9 of the nine numbers, and vertical, horizontal, oblique upward of the sum of three numbers are equal. A similar problem is mathematically called the magic side. Nine Gongge in the present language to describe, is a 3x3 magic square problem.

If you are programming with C + + or Python, how will you solve this problem? If I do not tell you the so-called "nine Gongge" words, what you think of course is to tell the computer how to do. In fact, even if I tell you the formula, you still have to let the computer according to the formula to step by stage implementation. Of course, because we're not here today to talk about imperative languages, we don't specifically demonstrate the code under C + + or Python. Below we give the implementation code of the Prolog. Of course, given that the reader may have never been in touch with Prolog, I added a note.

:-Ensure_loaded (Library (CLPFD)). Puzzle_solution (LS):-    % equal    test_rows (LS) per line and    number in% nine is not duplicated    Check_difference (LS),    sum_diagonal (LS, Sum1),    transpose (ls,ts),    % two diagonal and also equal    sum_diagonal (Ts, SUM1),    % of each column and are equal to    test_rows (Ts). sum_diagonal ([[h|_]|[]], H). sum_diagonal ([[h|_]| LS], sum):-    remove_heads (LS, x),    sum_diagonal (x, SUM0),    sum is H + sum0.remove_heads ([],[]). remove_ Heads ([[_| t]| Rows],[t| X]):-Remove_heads (Rows,x). Test_rows ([]). Test_rows ([[[]]). test_rows ([[_|_]]). test_rows ([x,y| Ls]):-     Initialize (x), Initialize (y),     sum_list (x, s), Sum_list (y, s),     test_rows ([y| LS]). check_difference (LS):-Append (LS, l), Is_set (l).% each of the cells is an integer from 1 to 9 initialize ([]). Initialize ([h| T]):-    H in 1..9, label ([H]),    Initialize (t).

first of all, this code is obviously short and simple. We didn't tell the computer what to do, we're just saying now we have a nine Gongge, and ask: (1) Each lattice is an integer from 1 to 9, (2) The numbers in nine are not duplicated, (3) each row is equal, (4) Each column is equal, and (5) Two is equal to the same on the diagonal.

Yes, actually we didn't do anything, just tell the computer what we want, and then let the computer think about what to do. And the computer did, as shown. Note that false is not a false meaning . Since the nine Gongge answer is not unique, although Prolog will find all the answers, but to demonstrate, we still hope that its solution space has a certain limit. So we set the two values of 2 and 5. Note that the matrix is represented in the form of an array equivalent to an array. And then for the first time, Prolog gives an answer, because we just want an answer, so we enter a period, which means OK, no more answers. Second execution, after Prolog give an answer, we want to ask whether there are any other answers, so we used a semicolon, the semicolon means to ask the computer there is no other answer . Prolog answered we said there was, so it gave an answer. (In fact, the two answers are the matrix of transpose) when we ask the semicolon again for more answers,Prolog answers, "There is no more answer," so it returns a false, so false means no more answers .

is not immediately feel prolog very powerful! In fact, Prolog also has a more powerful name, called Ai language, which is very common in writing expert systems and artificial intelligence applications, and in the future, bloggers will be able to offer more and more dazzling prolog.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

See Martial Arts Programming _ nine Gongge as an example introduce powerful declarative language prolog

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.