Scala functional Programming Design principle first lesson programming paradigm (programming Paradigms)

Source: Internet
Author: User

I've been using Scala for a year or two, and the language seems to have a magical power that people don't want to let go of when they use it. Scala has had a very profound impact on my entire program life, and I learned about functional programming, so I know that there is an elegant, efficient, powerful language in the world.

Scala is very popular abroad, but somehow it's always tepid at home, and I want to do something about the development of the language in Scala in China. Don't dare to talk about Scala's programming experience, because to become a Scala great God still has a long way to go, had to translate a Scala video tutorial readers, we found that the wrong place, please a lot of criticism advice.

The author of this video is the author of Scala, Martin, and the source is Coursera.

The following official start:

You are very welcome to participate in my "Scala Functional Programming Design principles" course. As the name says, we are going to learn quite a bit about Scala's programming knowledge, but this is not the main purpose of this course, the main purpose of this tutorial is to teach you to switch from your first programming paradigm (Mother programming paradigm) to functional programming paradigm, you will see some functional programs, Learn how to build these functions, and why you should write the program. You will find that the functional programming paradigm is a very different programming paradigm from Java or C programming paradigms, in fact, you can combine these two programming paradigms, which is one of the advantages of Scala, so that it provides a lightweight programming paradigm transformation, Allows you to convert from other programming paradigms into purely functional programming. We are not going to follow up on this class, we have to relax and have a look at what functional programming is like and let you open your eyes. In this way, I believe you can better absorb this programming idea, once you start to learn the function of the program, you will be able to integrate functional style into your daily programming. In fact, Scala is ideal for people who are transformed from object-oriented programming paradigms to functional programming paradigms, and Scala combines the two paradigms very well. OK, here we go:

The functional programming paradigm is a different programming paradigm, and if we return to the meaning of the word "programming paradigm", we need to explain:

"Programming Paradigm": "Paradigm" is scientifically described as a concept that is clearly described or a way of thinking in scientific practice. The main "programming paradigms" are: "imperative programming paradigm (imperative programing)", "functional programming paradigm (functional programing)", "Logical programming paradigm (Logic programing)". Imperative programming Paradigm Language: Java or C, etc. The logical programming paradigm is usually not mentioned, and there is a paradigm that runs through the exception of these three programming paradigms: "Object-oriented programming paradigm (object-oriented programing)". Because the object-oriented programming paradigm can be arbitrarily combined with these three main programming paradigms, such as object-oriented and functional, the combination is fairly good.

Let's review what is imperative programming paradigm--imperative programing.

There are several features of imperative programming:

1, define variable variable, can modify variable variable

2, the use of various assignment values. (using assignments)

3, the use of control statements such as: If-then-else, looping statements (loops), break statements, continue, return and so on

It is generally thought that the best informal way to understand the imperative programming paradigm is to understand von Joyman computer's instruction sequence, which is the originator of imperative programming. Thus, a Von Joyman computer, which basically contains a message processor and a memory, is the main line between memory and processor (bus), and the main line is used for communication between the two. Most importantly: the width of the main line is about the size of a machine word, which is now 32 or 64 bits. Therefore, there is a situation, due to the current computer architecture impact, now the program is not a small order of magnitude, are all large orders of magnitude.


Many of the concepts in computer languages now have a lot to do with the Von Joyman machines of the Year:

Variable variable = = = Memory Unit

Reference to variable = = = Load Instruction

Variable assignment = = = Storage Instruction

Control Statement = = = Jumps (similar to a compiled jump command)


The question is: how do we avoid this conceptualization of a word in a word programming way? We want to push the program in a big organization, rather than a word-by-word narrative. The argument was put forward by John Backus, notably: John Backus was actually the first computer high-level language creator, called Fortran in about 1950 years, and after about 20 years, he found that the traditional imperative programming model was not suitable, He needs new programming patterns, and this new programming paradigm is functional programming. At the time, John Backus said that the purely imperative programming model would be limited by the Von Joyman machine-we use a word-by-word construction data, and if we want to expand, we have to define more advanced programming ideas such as: Set (Collections), multiple (polynomials), Geometry (geometric), shapes (shapes), strings (strings), files (documents), and so on. For a thorough solution, we need a theory to interpret these sets (collections), multiple (polynomials), Geometry (geometric), shape (shapes), strings (strings), documents, and so on. So that we can better write and use them. So, what is this theory? This theory contains:

1. One or more data structures

2. Operation of these data structures

3. Describe the principles of these values and the operation relationship

Normally, this set of theories is not recommended for variable! (mutable means things that we can change, such as mutable variables) that is to say, the theoretical system of things are no side effects, can be reused, the results are consistent!

Here is an example of the summation of the two polynomial according to the theoretical rules:

But it does not define an operator to change a factor while keeping the polynomial the same!

In an imperative program, you can write this:

We can assign a value of 42 to the first number of polynomial P's coefficient, but this p is still the same p, which is not desirable in mathematics, and will offend the grammar and the theoretical system. Let's look at another example:

Suppose that the strings string of the theory defines a connector "+ +", and the connector is so combined:


However, this theory does not define an operator to change the elements in a sequence, so the string sequence is always the same! (At this point, some languages do the right thing, for example: The Java string is immutable.) )

If we want to fulfill this high-level concept and follow the mathematical theory, there will never be a variable!

1, this set of theoretical system is not allowed to appear variable!

2. The variable amount can destroy the grammatical rules of the theoretical system

So, let's:

1, the operator can be defined as a function, the operator is a function, you can define a number of operators.

2. Rejecting mutable variables

3, there are some powerful ways to abstract and build functions

Therefore, the first step in starting functional programming means rejecting mutable variables. In fact, there are two ways to understand functional programming, a constrained dimension and a more general dimension.

At a constrained level: functional programming means programming without mutable variables, assignments, loops, and other brilliant control statements.

On a more general level: Functional programming means focusing attention on functions.

In particular, a function is a value that can be produced, consumed, and assembled.

All of the above is very simple in a functional computer language.

Thus, we can recognize functional programming languages on two levels:

Restricted plane: A functional programming language is not variable, cannot be assigned, and has no command-style control structure.

A more general dimension: a functional programming language can focus on building elegant functions.

In particular, a function is a class-one citizen of a functional programming language, which means that functions can be defined anywhere, including other functions; As with other values, functions can be passed as arguments, and, like other values, there is a set of operators to go to component functions.

Here are some of the functional programming languages:

Restricted: Pure Lisp family of languages (Pure Lisp, XSLT, XPath, XQuery, FP), Haskell (without I/O Monad or Unsafeperformio)

More popular: Lisp family of Languages (Lisp, Scheme, Racket, Clojure), ML Family of languages (SML, Ocaml, F #), Haskell, Scala



History of Functional programming languages:


Functional programming is becoming more and more popular because it provides these advantages:

1, a more concise way of expression

2. Better modularity

3, very good at multi-core parallel development or cloud computing operations


Scala functional Programming Design principle first lesson programming paradigm (programming Paradigms)

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.