3.1.1 introduction-to-functional-programming

Sangmin Ahn
4 Feb 202404:14

Summary

TLDRこのスクリプトは、クラスでの講義や法的機関でのフォーマルな言語と日常会話でのカジュアルな言語の使い方の違いから、コンピュータ言語におけるプログラミングパラダイムの多様性について説明しています。特に、関数型プログラミング(FP)とオブジェクト指向プログラミング(OOP)の二つのパラダイムに焦点を当て、データと関数の明確な区別、データの関数外での存在、および関数によるデータ操作について説明しています。通貨変換のコード例を通じて、関数型プログラミングの実践的応用が示されています。

Takeaways

  • 📚 フォーマル言語とカジュアル言語の使い分け:教室での講義や法的機関での会話はフォーマルで、文法規則を守り、複雑な語彙を使用。
  • 🗣️ 人間の日常会話はスラングが多く、文法規則を曲げたり破ったり、会話は口語的。
  • 🌐 人間の言語には多くのスタイルがあり、全ては同じ機能、すなわちコミュニケーションを果たす。
  • 💻 コンピュータ言語も同様に様々なスタイル、すなわちプログラミングパラダイムを持っており、異なるアプローチを提供。
  • 🔄 機能プログラミングとオブジェクト指向プログラミング(FPとOOP)は、プログラミングの2つの一般的なパラダイム。
  • 📊 データと関数の明確な区別:関数プログラミングでは、データは関数から独立して存在できる。
  • 🔢 関数の定義と引数の渡し方:関数は引数を介してデータを受け取り、処理結果を返す。
  • 🏦 オブジェクト指向プログラミングでは、データと関数はオブジェクトに結合される。
  • 💰 通貨変換の例:関数プログラミングを使って、通貨の交換レートを計算する実用的な例。
  • 📈 カーソルケースでの変数の宣言:CamelCase(キャメルケース)を使用し、最初の単語は小文字、それ以降は大文字。
  • 📊 関数のテストとデバッグ:関数の実行結果をコンソールにログ出力することで、プログラムの正しさを確認する。

Q & A

  • レクチャーでの言語使用はどのようにして正式的呢?

    -レクチャーでの言語使用は、文法規則が遵守され、構文と語彙がトピックに基づいて非常に複雑になることが一般的です。

  • フォーマル言語が使用される他の場面にはどのようなものがありますか?

    -フォーマル言語は、裁判所やその他の法的機関などで使用されることがあります。

  • 日常会話においてスラングが存在する理由は何ですか?

    -日常会話では、文法規則が変形されることが多く、カジュアルなトーンで話すことが一般的です。そのため、スラングやカジュアルな表現が使用されます。

  • コンピュータ言語におけるスタイルの違いは何を指しますか?

    -コンピュータ言語におけるスタイルの違いは、プログラミングパрадиグムと呼ばれます。これらはコードを書く際の異なるアプローチを指し、コンピュータに操作を実行する結果は同じになります。

  • 関数型プログラミングとオブジェクト指向プログラミングの違いは何ですか?

    -関数型プログラミングでは、データと関数が明確に分離され、データは関数の外側に存在することができます。一方、オブジェクト指向プログラミングでは、データと関数がオブジェクトに組み合わされています。

  • 関数型プログラミングにおけるデータと関数の関係はどのようになっていますか?

    -関数型プログラミングでは、データと関数との間に明確な境界があります。データは関数の外側に存在し、関数は与えられたデータに基づいて計算を実行します。

  • 通貨変換プログラムの例で使用された変数名の命名規則は何ですか?

    -通貨変換プログラムの例では、変数名がCamelCaseという命名規則を使用しています。これは、最初の単語の最初の文字を小文字にし、その後の単語の最初の文字を大文字にするものです。

  • 通貨変換関数 `convertCurrency` はどのように定義され、どのような仕事をしていますか?

    -通貨変換関数 `convertCurrency` は、2つの引数、amountとrateを受け入れる関数です。関数の体内では、引数として与えられた金額を率で掛け合わせた結果を返します。

  • 通貨変換プログラムで `currencyTwo` の値を更新するためにどのような手順が取られましたか?

    -通貨変換プログラムでは、`currencyTwo` の値を更新するために、`convertCurrency` 関数の結果を `currencyTwo` に代入しました。また、必要な2つの引数(`currencyOne` と `exchangeRate`)を渡すことで、関数が正しく動作するようにしました。

  • プログラムの最後に `currencyTwo` の値を出力するためにどのような手法が使用されましたか?

    -プログラムの最後に、`currencyTwo` の値をコンソールにログ出力するために `console.log` 関数が使用されました。これにより、通貨変換の結果である120がコンソールに表示されます。

  • このレッスンで学んだ関数型プログラミングの利点は何ですか?

    -このレッスンで学んだ関数型プログラミングの利点は、データと関数の分離により、問題を解決するための柔軟性とコードのメンテナンス性を高めることです。

Outlines

00:00

📚 フォーマル言語とプログラミングパラダイム

このパラグラフでは、フォーマル言語と日常言語の違いを探求し、それがプログラミング言語にどのように当てはまるかを説明しています。講義での言語はフォーマルで複雑ながら、日常言語はスラングや規則の自由な使用が特徴です。同様に、プログラミング言語にはさまざまなスタイル、つまりプログラミングパラダイムが存在します。代表的な二つのパラダイム、関数型プログラミング(FP)とオブジェクト指向プログラミング(OOP)が紹介され、それぞれのスタイルが異なるアプローチを提供することが説明されています。これらはコンピュータに操作を指示するという同じ目的を果たします。本レッスンでは、関数型プログラミングに焦点を当てています。

Mindmap

Keywords

💡formal language

「formal language」とは、正式な場面や状況で使用される言語のスタイルを指します。このスタイルは文法規則を厳密に遵守し、語彙や文法が複雑になることが多いです。例えば、講義や法的機関での会話がこれに該当します。ビデオスクリプトでは、教室での講義や法廷での使用が例として挙げられています。

💡colloquial

「colloquial」とは、日常会話で使用される比較的カジュアルな言語スタイルを指します。このスタイルでは、スラングや文法のルールが緩められることが多く、自然な流れで話すことができます。ビデオスクリプトでは、日常の会話と比較して説明されています。

💡programming paradigms

「programming paradigms」とは、プログラミングにおいて使用される異なるスタイルやアプローチを指します。これらのパラダイムは、問題解決に様々な方法を提供しますが、どちらが他者よりも優れているわけではありません。ビデオスクリプトでは、関数型プログラミングとオブジェクト指向プログラミングが紹介されています。

💡functional programming

「functional programming」とは、関数を第一級オブジェクトとして扱うプログラミングのスタイルです。このスタイルでは、データと関数が明確に分離され、関数によってデータに作用することができます。ビデオスクリプトでは、通貨変換の例を使ってこのパラダイムを説明しています。

💡object-oriented programming

「object-oriented programming」とは、データと関数をオブジェクトにパッケージ化するプログラミングのスタイルです。オブジェクト指向プログラミングでは、データとその操作に関するメソッドが統合され、オブジェクト间的の相互作用を通じてプログラムが動作します。ビデオスクリプトでは、後ほど紹介されることになっています。

💡data and functions

「data and functions」は、プログラミングにおいてデータを操作するための関数を指します。データと関数の関係は、プログラミングの基本的な概念の一つで、どのようにデータが生成、変更、使用されるかを定義します。ビデオスクリプトでは、関数型プログラミングにおいてデータと関数が分離される点に焦点を当てています。

💡CamelCase

「CamelCase」は、プログラミングでよく使われる命名規則の一つです。この方法では、単語の最初の文字を大文字にし、それ以降の単語の最初の文字も大文字にして、スペースを入れずにつなげます。例えば、変数名「currencyOne」のように、先頭の単語は小文字で、その後の単語の先頭は大文字になります。ビデオスクリプトでは、変数宣言の例として使用されています。

💡currency conversion

「currency conversion」とは、異なる通貨間で金額を変換するプロセスを指します。これは、国際取引や旅行などで頻繁に行われます。ビデオスクリプトでは、通貨変換を例に挙げ、関数型プログラミングを使ってこの問題を解決する方法を説明しています。

💡variables

「variables」とは、プログラミングにおいてデータを格納するための記憶領域を指します。変数は、値を保存し、プログラムの実行中に変更されることができます。ビデオスクリプトでは、通貨の金額や交換レートを格納するために変数が使用されています。

💡functions

「functions」とは、プログラミングにおいて特定のタスクを実行するために使われるコードのブロックを指します。関数は引数を受け取り、処理を実行し、結果を返すことができます。ビデオスクリプトでは、通貨変換を実行する関数が定義されています。

💡arguments

「arguments」とは、関数を呼び出す際に渡される値を指します。引数は、関数が期待するデータを提供し、その関数が正しく動作するため必要な要素です。ビデオスクリプトでは、通貨変換関数において、通貨の金額と交換レートが引数として使われています。

💡console log

「console log」とは、コンソールにメッセージを出力するプログラミングの操作を指します。これにより、プログラムの実行中に何が起こっているかを確認することができます。ビデオスクリプトでは、通貨変換の結果をテストするために使用されています。

Highlights

Language used in a lecture is formal, adhering to grammar rules and complex syntax based on the topic.

Formal language is also used in legal institutions such as courts.

Everyday conversational language is full of slang and often bends or breaks grammar rules, being more colloquial.

Human languages have various styles, all serving the same function of communication.

Computer languages, like human languages, have different styles known as programming paradigms.

Functional programming (FP) and object-oriented programming (OOP) are two commonly used programming paradigms.

No programming paradigm is inherently better than the other; they are just different approaches to writing code.

Functional programming distinguishes clearly between data and functions, allowing data to exist outside of functions.

In functional programming, data is passed to functions as arguments, which then perform operations and return values.

Object-oriented programming combines data and functions into objects.

The lesson focuses on functional programming and its practical implementation.

A coding example is provided to demonstrate a currency conversion using functional programming.

Variables are declared with CamelCase naming convention in the example, starting with a lowercase letter and subsequent words starting with uppercase.

The 'convertCurrency' function is defined, taking 'amount' and 'rate' as parameters and returning their multiplication.

The value of 'currencyTwo' is updated using the result of the 'convertCurrency' function, demonstrating the use of functions in manipulating data.

The 'currencyOne' and 'exchangeRate' variables are passed to the 'convertCurrency' function to perform the currency conversion calculation.

The final result of the currency conversion is logged to the console, showcasing the practical application of functional programming in solving a problem.

The lesson concludes by emphasizing the separation of data from functions in functional programming as an effective problem-solving approach.

Transcripts

play00:00

When you listen to a lecture in class,

play00:02

the language used is probably very formal.

play00:05

Grammar rules are observed,

play00:06

and syntax and vocabulary can

play00:08

get quite complex based on the topic.

play00:10

Some other examples of the use of

play00:13

formal language can be found in

play00:14

courts and other legal institutions.

play00:17

Now let's consider the way people

play00:18

talk in their everyday life.

play00:20

There's lots of slang,

play00:22

grammar rules are bent and broken,

play00:24

and the entire delivery is colloquial.

play00:27

There are many styles in every human language,

play00:29

and they all perform

play00:30

the exact same function, communication.

play00:33

The same can also be said about computer languages.

play00:37

They too have various styles,

play00:39

also known as programming paradigms.

play00:41

Just like in human languages,

play00:43

no one style is better suited than the other.

play00:46

In programming, there are two commonly used paradigms.

play00:50

Functional programming, sometimes abbreviated as FP,

play00:54

and object-oriented programming,

play00:57

sometimes abbreviated as OOP.

play01:00

You can think of these paradigms as

play01:02

different approaches to writing code.

play01:04

But the result is still the same,

play01:06

instructing a computer to perform a set of operations.

play01:09

In this lesson, you will

play01:11

learn about functional programming.

play01:13

Let's now focus on the functional programming style.

play01:16

There is a clear distinction

play01:18

between data and functions in

play01:20

functional programming as data

play01:21

can exist outside of functions.

play01:23

For example, you may recall that when functions need

play01:27

some data you pass them

play01:28

the values in the form of arguments.

play01:30

Then the function perform some work

play01:32

on the given data and returns

play01:34

some values that you can then

play01:35

use somewhere else in your program.

play01:38

An alternative paradigm is

play01:39

the object-oriented programming paradigm,

play01:42

where you combine both data and functions into objects.

play01:46

You'll learn more about

play01:47

object oriented programming later.

play01:49

Now let's examine a practical implementation of

play01:52

the functional programming style using

play01:55

a coding example to calculate currency conversion.

play01:58

In this example of functional programming,

play02:01

let me start by giving my program some data.

play02:04

The purpose of the program is

play02:06

to perform currency conversions.

play02:08

Notice that I already have a variable declared with

play02:11

the name currencyOne and assigned it the value of 100.

play02:15

Next, I'll add two more variables, currencyTwo,

play02:19

which is assigned a value of 0,

play02:21

and exchangeRate, which is set to 1.2.

play02:25

Notice that I have declared

play02:27

all these variable names using CamelCase.

play02:30

This is where the first letter of

play02:32

the first word is lowercase and

play02:34

the first letter of the subsequent words are

play02:36

without spaces and in uppercase.

play02:38

Next, I can create the function that will

play02:41

work with these variables and operate on this data.

play02:44

First, I create a function named

play02:46

convertCurrency that accepts two parameters,

play02:49

amount and rate.

play02:51

Then in the function's body,

play02:53

I returned the result of

play02:54

the amount multiplied by the rate.

play02:56

Basically, I'm coding it to multiply

play02:59

the two values it receives and return the result.

play03:02

Next, I need to update the value of currencyTwo,

play03:05

which was previously defined on line four,

play03:07

assigned to the value of 0.

play03:10

To do this, I assign the value of

play03:12

currencyTwo to the result of the function I just created.

play03:15

Recall that when the function is invoked,

play03:18

it will return a value which will be

play03:20

assigned to the variable currencyTwo.

play03:23

It's important to remember that I need to also

play03:26

pass the two arguments required for my function to work.

play03:29

In this example, I'm sending

play03:31

the variables currencyOne and exchangeRate.

play03:35

Basically, I'm passing the values 100 and

play03:38

1.2 to the function so it

play03:41

can perform its calculation and return the result.

play03:44

Finally, I console log the value

play03:47

of currencyTwo to test my code.

play03:49

Success, 120 is output to the console,

play03:53

the result of 100 multiplied by 1.2.

play03:57

In this lesson, you learned about

play03:59

functional programming and how it can be used

play04:01

to solve a problem by separating

play04:03

data from functions. Great job.

Rate This

5.0 / 5 (0 votes)

Related Tags
プログラミングフォーマル言語ファンクショナルオブジェクト指向コード例通貨換算データと関数教育的技術入門プログラミング初心者
Do you need a summary in English?