The colon and his students (serial 6)--Basic paradigm

Source: Internet
Author: User

Basic Paradigm

The greatest goal in life is action-Rockefeller's advice

At the beginning of the second lesson, the colon came to the point: "The first is the basic two programming paradigms: imperative and declarative, where the imperative is called a procedure." Popular point says, imperative programming consists of order sequence, namely a series of imperative sentence: ' Do this first, then do that ', emphasize ' how to do '; declarative programming consists of related expressions, a series of declarative sentences: ' Know this, solve that ', and emphasize ' what to do '. Academic point of view, imperative programming is the computer (von Neumann machine) operating mechanism of abstraction, that is, orderly access to the instructions and data from memory and then to execute; declarative programming is the abstraction of the human brain's way of thinking, that is, using mathematical logic or established norms (specification) to perform inference on known conditions ”

Quotation marks Mutter: "Most languages seem to be imperative." ”

Colon interface: "The evolution of language is gradual, most of the language traced to the assembly language upgrade, and as the machine language one by one corresponding to the assembly language is naturally imperative, so this paradigm is most traditional and universal." Declarative language is originated from the research of artificial intelligence, mainly including functional language and logical language. In fact, their appearance is no more than the order of the late--the earliest functional language Lisp (LISt Processor) has half a century of history, the earliest one of the logical language Prolog (programming in LOGic) and the same age of C. Just because most of the more is used for academic research rather than for commercial applications, some ' purdah are not known '. The difference of origin determines that these two paradigms represent very different programming concepts and styles: imperative programming is action-oriented (action-oriented), so the algorithm is explicit and the target is recessive; declarative programming is a target-driven (Goal-driven), Thus the target is dominant and the algorithm is recessive. For ease of explanation, the following three representative languages are used to implement factorial (factorial) operations. ”

The colon is projecting on the blackboard--

C (Command type):

int factorial(int n)
{
   int f = 1;
   for (; n > 0; --n) f *= n;
   return f;
}

Lisp (functional):

(defun factorial(n)
(if (= n 0) 1             // 若n等于0,则n!等于 1
   (* n (factorial(- n 1)))))   // 否则n!等于n* (n-1)

Prolog (logical):

// 0! 等于1
factorial(0,1).
// 若M等于N-1且 M!等于Fm且F等于N*Fm,则N! 等于F
factorial(N,F) :-  M is N-1, factorial(M,Fm), F is N * Fm.

The colon asks: "Apart from the grammatical details, how do you say the above three code is different?" ”

The full stop mused for a moment and replied: "C explicitly gives the iterative algorithm of factorial, while Lisp only describes the recursive definition of factorial, and the Prolog states two assertions about factorial." ”

The colon is very satisfied: "In the language!" Second question: Which way of thinking are you more accustomed to? ”

The comma is not thinking: "Of course the first!" ”

The colon smiled and said, "This proves that you are at least a trained programmer." Recall that when you are learning to program, are you accustomed to this way of thinking? ”

The exclamation mark mused: "I don't seem to be accustomed to statements like i = i + 1." ”

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.