Home  >  Article  >  What are the multi-paradigm programming languages?

What are the multi-paradigm programming languages?

coldplay.xixi
coldplay.xixiOriginal
2020-12-07 14:25:213830browse

Multi-paradigm programming languages ​​include: 1. Object-oriented programming, which improves the reusability, flexibility and scalability of software; 2. Functional programming, a programming paradigm that uses mathematical functions as the core programming language modeling ; 3. Generic programming provides a higher level of abstraction.

What are the multi-paradigm programming languages?

#The operating environment of this article: Windows 7 system, Dell G3 computer.

Multi-paradigm programming languages ​​are:

Summary:

This article mainly talks about three programming paradigms - object-oriented programming and functional programming , The concept of generic programming

Programming paradigm

The programming paradigm is the idea behind the programming language. Represents the program designer's view of how the program should be constructed and executed. Common programming paradigms include: procedural, object-oriented, functional, generic programming, etc.

Some programming languages ​​are specially designed for a specific paradigm. For example, C language is a procedural programming language; Smalltalk and Java are relatively pure object-oriented programming languages; Haskell is a purely functional programming language. In addition, the relationship between some programming languages ​​and programming paradigms is not one-to-one. For example, Python, Scala, and Groovy all support object-oriented and functional programming to a certain extent. C is a successful example of a multi-paradigm programming language. C supports the same procedural programming paradigm as the C language, and also supports the object-oriented programming paradigm. STL (Standard Template Library) enables C to have generic programming capabilities. Supporting multiple paradigms may be one of the reasons why C still has such a strong vitality to this day.

Swift is a typical multi-paradigm programming language, which supports object-oriented programming paradigm, functional programming paradigm, and generic programming. Swift's support for multiple programming paradigms is determined by its creation goals. The original intention of Swift was to provide a practical industrial language. It is different from academic programming languages ​​like Haskell that come from universities and research institutions. Apple had a clear business purpose when it launched Swift: Objective-C, the main programming language for Mac OS and iOS systems, has become obsolete. Swift will allow developers of Apple systems to have a more modern programming language, thereby promoting Apple The healthy development of the entire ecosystem.

The design and development of Swift all reflect the goal of "practical industrial language". This determines that Swift cannot do extreme language experiments. It needs to carefully seek breakthroughs based on facing reality rationally. This determines that Swift needs to inherit the historical legacy, take care of the practical needs of most programmers today, and at the same time develop towards the future.

1. Object-oriented - inheriting the legacy

Object-oriented programming uses objects as the basic unit of the program, encapsulating the program and data in it to improve the reusability of the software. Flexibility and scalability.

The core concept of object-oriented programming:

  • Polymorphism refers to different related classes generated by inheritance, whose objects will respond to the same message differently. Response;

  • Inheritance, in some cases, a class will have "subclasses". The subclass is more specific than the original class (called the parent class);

  • Encapsulation, object-oriented programming hides the specific execution steps of a certain method, instead using messages The delivery mechanism delivers messages to it.

In an object-oriented programming language, objects are used to build the basic unit of the program. Polymorphism provides higher abstraction capabilities, allowing us to design more general programs. Inheritance provides a way to reuse code. Encapsulation provides a more convenient and safer mechanism to use other code.

The working language of most programmers is still object-oriented programming language. Most popular modern programming languages ​​allow you to create objects. Software models are easy to build using object-oriented programming languages. Because objects are classes it seems easy to relate to all things and concepts in the real world. But programming practice shows that not everything becoming an object is a good thing. To give a lame example in Java: only objects can be passed into functions as parameters in Java (and of course primitive types). So in order to pass a function to another function, you need to wrap the function in an object, usually with an anonymous class, because this class has no other purpose, just to make Java's everything-object design happy.

Java has a pure object-oriented concept. From the beginning of its design, it was hoped to model the world with a pure object model in which everything is an object. But now, more and more non-object things have been added to Java. Closures were introduced to obtain first-level functions in functional programming; generics were introduced to obtain parameterized types. This may suggest that the world is so rich and diverse that modeling it using a single model will not be successful.

2. Functional programming - development

Functional programming is a programming paradigm that uses mathematical functions as the core programming language modeling. It treats computer operations as mathematical function calculations and avoids the use of program state and mutable objects.

There are two main ideas in functional programming:

  • Using functions as the core of programming language modeling;

  • Avoid state and mutability.

Functions are the cornerstone of functional programming. The code of a functional programming language is composed of functions. The process of writing a functional language is designing functions. Large-scale programs are composed of thousands of functions, and these functions must be combined efficiently. Functional programming languages ​​try to avoid states and mutable objects. The absence of mutable state makes functions in functional languages ​​pure functions. Pure functions are easier to modularize, easier to understand, and friendly to reuse.

Functional programming languages ​​have also produced some useful programming tools:

  • First-level functions, closures;

  • Curried function;

  • lazy evaluation.

These will be mentioned in subsequent chapters. These programming tools are increasingly appearing in other programming languages ​​as well.

Functional programming language is not young, its history is as long as object-oriented programming. LISP, created in 1958, is the oldest functional programming language. It is older than the C language. But it is only recently that functional programming ideas have gradually been taken seriously. Almost all newly invented programming languages ​​are more or less influenced by functional programming ideas. Python, Scala, Groovy, and Swift all have first-level functions and closures. This allows you to pass a function directly to another function, and the function can also be returned by another function in the form of a return value. The benefits of eliminating state and providing immutability are increasingly accepted. Scala, Groovy, and Swift all provide convenient methods for declaring immutable objects to support you in writing code that is closer to a functional style.

Functional programming language has its advantages and may become an important programming paradigm in the future. However, the importance of functional programming languages ​​may be more reflected in the development of other programming languages. In the future, it may be difficult for a programming language designed primarily with a functional programming paradigm to become a mainstream programming language. Programming languages ​​like Java that are built on a single programming paradigm (object-oriented) have little chance of becoming mainstream. The pursuit of a purely functional programming language like Haskell may be more of an academic language experiment.

Repeat the reasons mentioned in the previous section, the world is so rich and colorful, modeling the world using a single model will not succeed.

Object-oriented and functional programming

If we classify the currently popular languages ​​according to language paradigms. Supporting object-oriented programming languages ​​should be the longest queue. Most of the popular modern programming languages ​​are object-oriented, and they all allow you to create objects. But at the same time, you will find that several of the more popular programming languages, Python, Scala, and even Java, are more or less influenced by functional programming languages. They all introduce some functional programming concepts, allowing you to write functional-style code to a certain extent.

After getting familiar with object-oriented programming languages, when you come into contact with functional programming languages, you will often find it refreshing, and you may even vaguely feel that functional languages ​​are a good way to save the world. So should we completely switch to functional programming languages? Use Haskell to save the world.

After large-scale practice of object-oriented programming languages, we do have a deeper understanding of their shortcomings (for example, it is difficult to write software applications in a multi-threaded environment; inheritance is not a good method for code reuse). Functional languages ​​do have many advantages, some of which can solve the problems of object-oriented languages ​​(pure functions are very suitable for multi-threaded environments, pure functions are inherently modular, and are very friendly to code reuse). However, functional programming may also have certain problems. These problems may only be exposed after larger-scale industry practice. We have now established that modeling the world in terms of objects alone is difficult. Then modeling the world with mathematical models may not be any better. What is certain is that they all have their own areas and environments in which they excel. We still don't have a programming paradigm that can solve all problems.

The bigger reality is that countless companies have made huge investments in object-oriented programming languages. Even though object-oriented programming has exposed some problems, functional programming has shown many solutions to these problems. The advantages of the problem are that no prudent person will and cannot abandon object-oriented programming immediately and completely and comprehensively switch to functional programming languages.

The realistic choice is to support object-oriented programming and provide functional support at the same time. In this way, you can still use object-oriented methods in most places where you are comfortable with object-oriented. And where functional programming is suitable, and you have functional programming thinking and ability, you can still improve productivity using functional programming methods.

3. Generic programming-beautiful embellishment

Generic programming is another interesting topic. Generics provide a higher level of abstraction for programming languages, namely parameterized types. In other words, it is to abstract the type information in an algorithm or class that is originally specific to a certain type. This abstracted concept is a template in C's STL (Standard Template Library). STL demonstrated the power of generic programming and became a powerful weapon of C as soon as it appeared. In addition to C, programming languages ​​such as C#, Java, and Haskell have introduced the concept of generics.

Generic programming is a slightly more local concept, it only involves how to deal with types more abstractly, that is, parameterized types. This is not enough to support the core concepts of a language. We don't hear about a programming language being purely generic programming with no other programming paradigms. But precisely because generics do not change the core of the programming language, most of the time, they can be well integrated into other programming methods. Programming languages ​​with different styles such as C, Scala, and Haskell all support generics. Generic programming provides a higher level of abstraction, which means greater expressive power. This is a delicious accompaniment to most programming languages.

In Swift, generics are widely used, and many of the Swift standard libraries are built with generic code. For example, Swift’s array and dictionary types are both generic sets. Examples like this can be found everywhere in Swift.

Summary

In these series of articles, we will mainly use Swift as an example to explain the multi-paradigm programming language. This series of articles is divided into three parts to discuss the three programming paradigms supported by Swift:

  • Object-oriented programming paradigm

  • Functional programming paradigm

  • Generic Programming

For more related knowledge, please visit the FAQ column!

The above is the detailed content of What are the multi-paradigm programming languages?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn