Functional Programming(1)

Artificial Intelligence Machine Learning Digital Transformation Clojure LISP Javascript Programming Technology ICT Technology Web Technology Programming Overviews Navigation of this blog
Functional Programming(1)

In the previous article, I described the evolution from a language directly connected to machines to a high-level language close to natural language, and then from a structured language to an object-oriented language (OOP) in the direction of productivity improvement. In this article, I will discuss functional languages, which are a different approach to productivity improvement from OOP.

In order to explain what a “function” is in a functional language, I will first define a function in mathematics as a “pure function”. As shown in the figure below, a pure function is characterized by the fact that when an input is input, a unique output corresponding to the input is obtained. This can be expressed as the function being represented by the mathematical expression y=f(x), where a specific value x is input and a corresponding unique value y is returned. This property is called “referential transparency”, and the term “no side effects” is used as a property to ensure this referential transparency.

A side-effect is an action other than the original action of the function, which is to return a value after inputting a value, such as a destructive assignment statement to return the value of a variable (when y=ax+b, a and b (internal state) change at a certain point), destructive change of an element of an array (the original value is lost), or a destructive change of a variable. They can be used to write to files, put values into databases, display images, etc., and are commonly used functions in general programming languages. In other words, a side effect is a change in the internal state of a function when it is considered as shown in the figure below.

はじめてのLISP関数型プログラミングより

Having this state is a basic function of computer technology, such as the Turing machine’s operation with side effects on the tape, or the Neumann-type computer’s operation with programs and data stored in memory (causing side effects).

Side effects are an essential part of programming, but if they are used unnecessarily, they can cause uncontrollable data changes in the program, which can lead to serious bugs. Controlling these side effects is an important factor to increase the productivity of programming, so for example, in OOP, side effects are controlled by designing well local and global variables in a class.

In this OOP approach, in order to reuse modules effectively, the designer’s intention of what distinguishes them must be kept together with the class, and in the case of complex programming, it takes a lot of time to record and check these intentions. To solve these problems, we developed an object-oriented programming system.

In order to solve these problems, the object-oriented approach, which had functions in each data structure as shown below, was changed to

clojureの世界観*1より

As shown in the figure below, “It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures. (It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures)” is the fundamental idea of functional programming.

Clojureの世界観*1より

With such a simple structure, programming can be done as if it were done with Lego blocks as shown below. The following figure is an excerpt from “simple made easy” by Rich Hickey. In this figure, the upper-left castle is made with yarn work, and the lower-right one with Lego blocks. The one made with yarn work is introduced as an analogy of OOP programming, and the one made with Lego blocks as an analogy of functional programming.

simple make easyより

However, the minimum unit differs between objects (classes) and functions, and functions are superior to objects in terms of “Ease to undersatnd,” “Ease to change,” and “Flexibility (Policy, location, etc.). The minimum unit is different between objects (classes) and functions, and functions are superior to objects in “Ease to undersatnd”, “Ease to change”, and “Flexibility (Policy, location, etc.)”, so simple and easy programming is possible.

In this article, I focused on the differences between functional languages and OOP. In the next article, I would like to discuss their features from the perspective of the history of functional languages.

Reference: 1) Worldview of Clojure

 

 

コメント

Exit mobile version
タイトルとURLをコピーしました