💡Block
Tags: functional, math, tutorial
Published: 2024-01-17 09:15:35

![image](https://raw.githubusercontent.com/ken-okabe/web-images4/main/img_1705992520504.png)

- #### [Unknown Unknowns "the ones you don’t know you don’t know"](https://dev.to/ken_okabe/unknown-unknowns-the-ones-you-dont-know-you-dont-know-37bc)

- #### [Functor and Monad at a Glance](https://dev.to/ken_okabe/functor-and-monad-at-a-glance-22a)

- #### 💡[Block](https://dev.to/ken_okabe/block-2nka)

---

<img width="500px" src="https://raw.githubusercontent.com/ken-okabe/web-images/main/img_1684706440396.png">

#### Introduction to outline of functional programming

---

- **Functional code is assembled like LEGO blocks**

- The basic principle of LEGO blocks is to "connect two **blocks** together to create a new block"

---

- The **blocks** of **functional code** are actually **expressions (mathematical expressions)**

<img width="500px" src="https://raw.githubusercontent.com/ken-okabe/web-images1/main/img_1691695638599.png">

---

- The way LEGO blocks are connected is **"fitting the bumps and holes"**, which is the only way, but the way **blocks of expressions** are connected is **infinite**

---

- When LEGO blocks are connected by **"fitting the bumps and holes"**, of course the **"shape and size of the bumps and holes"** must match, and such rules are called **standards** in the industrial world, and the **USB** standard is well known

![image](https://m.media-amazon.com/images/I/61hXFKt7qTL._AC_SY450_.jpg)

[Amazon affiliate to do UGREEN USB Type C Cable for Smartphone L-Shaped Nylon Braided 3A Fast Charging Quick Charge 3.0/2.0 Compatible 56K Resistor Installed Xperia XZ XZ2, LG G5 G6 V20 etc. Compatible (1m)](https://amzn.to/4434Vo2)

---

- **When two expressions (blocks) are connected by an operator, the operator and the expressions must match the standard**, which is called **standard** in the industrial world, but in the world of programming, it corresponds to something called **Type**

![](https://raw.githubusercontent.com/ken-okabe/web-images3/main/note.svg)

**The similarity between the "standard" of the industrial world and the "type" of the programming world**

*What is the "type" of the programming world, or more precisely, the "type" as a mathematical entity? We will dig deeper into this later, but as a rough image for the first entrance, you can understand it as something like the "standard" of the industrial world*

---

![image](https://raw.githubusercontent.com/ken-okabe/web-images3/main/img_1704442355605.png)

![image](https://raw.githubusercontent.com/ken-okabe/web-images3/main/img_1704443261596.png)

![image](https://raw.githubusercontent.com/ken-okabe/web-images3/main/img_1704443356988.png)

![](https://raw.githubusercontent.com/ken-okabe/web-images3/main/notefooter.svg)

---

- Since there are infinite ways to connect **blocks of expressions**, there are also infinite **types**

---

- Among the infinite ways of connecting **blocks of expressions**, the **four methods of addition, subtraction, multiplication, and division** are taught in elementary school arithmetic and are called **arithmetic operations**

![image](https://raw.githubusercontent.com/ken-okabe/web-images1/main/img_1691694857440.png)

---

- This way of connecting two expressions (blocks) is called **binary operation**, and arithmetic operations are members of binary operations

---

- The **(binary) operators** that connect two expressions (blocks) are  {% katex inline %}~+ - ~ \times ~ \div{% endkatex %}

![image](https://raw.githubusercontent.com/ken-okabe/web-images/main/img_1687328316202.png)

---

- The **type** that matches the **expressions (blocks)** to be connected is a number

{% katex %}
1 ~ 2 ~ 3 ~ 4
{% endkatex %}
---

- A binary operation that **connects two expressions (blocks) {% katex inline %}1{% endkatex %} and {% katex inline %}2{% endkatex %} with the operator {% katex inline %}+{% endkatex %}**

{% katex %}
1 + 2 = 3
{% endkatex %}
---

- A binary operation that **connects two expressions (blocks) {% katex inline %}3{% endkatex %} and {% katex inline %}4{% endkatex %} with the operator {% katex inline %}\times{% endkatex %}**

{% katex %}
3 \times 4 = 12
{% endkatex %}
---

- {% katex inline %}1 + 2 = 3{% endkatex %} is a **new expression (block) that is assembled by connecting two expressions (blocks)**

---

- Therefore, as long as you can connect two expressions (blocks), **it is just a repetition of the same thing**, and you can freely connect three or more expressions (blocks) by combining the same connection {% katex inline %}+{% endkatex %} or different connections {% katex inline %}\times{% endkatex %}, etc.

{% katex %}
(1 + 2)\times 4 = 12
{% endkatex %}
---

- **Functional programming** is a method of doing everything **only** by connecting and assembling blocks like this

---

- So in the end? Functional programming is just building mathematical expressions like {% katex inline %}(1 + 2)\times 4 = 12{% endkatex %}, so it can be said to be **mathematics itself**

---

- **Since we have already done this kind of operation in arithmetic and mathematics classes, we have already mastered the basics of functional programming!** 🥳🎉

---

![](https://raw.githubusercontent.com/ken-okabe/web-images3/main/note.svg)

**Programming languages where code is composed of expressions**

Relatively new programming languages such as Rust and Verse have a strong functional character, and in principle, code is composed of expressions

![image](https://raw.githubusercontent.com/ken-okabe/web-images3/main/img_1701326339175.png)

### Statements and expressions

>Rust is primarily an expression language. This means that most forms of evaluation produce or cause a value, and evaluation is guided by a unified syntax category called expressions. Each kind of expression can usually be nested within other kinds of expressions, and the rules for evaluating an expression specify both the value it produces and the order in which its subexpressions are themselves evaluated.

>In contrast, statements are primarily used to contain expressions and sequence their evaluation.

![image](https://raw.githubusercontent.com/ken-okabe/web-images3/main/img_1701327585125.png)

---

![image](https://raw.githubusercontent.com/ken-okabe/web-images3/main/img_1701327407248.png)

![image](https://raw.githubusercontent.com/ken-okabe/web-images3/main/img_1701327441106.png)

---

As explained in the references of Rust and Verse, which emphasize that **code can be written in expressions** at the beginning, the content is exactly the same as what was introduced in this chapter, and it is the most basic and fundamental nature of functional code

![](https://raw.githubusercontent.com/ken-okabe/web-images3/main/notefooter.svg)