見出し画像

Pascalの「写経」を始めました。

お疲れ様です。Y研究員です。個人開発も一通り終えたので、次にやることを探していました。参考書籍を見回った結果、アルゴリズムとデータ構造を勉強することにしました。

背景

C言語でざっと一通りやったとは有りますが、自分で書くコードの質を上げたいなと思ったからです。現代のプログラミングはライブラリやAPIをいかに上手く使うかが主問題になっています。しかし、まだ基礎のアルゴリズムとデータ構造が大事な場面は有ります。特に独自性の高い事を実現しようとすると必要になります。

アルゴリズムとデータ構造の教科書は山ほど有ります。調べてみると古典的な教科書の2大巨塔はどちらもPascalで書かれていると分かりました。Pascalは古い言語ですが「教育用」として流行っていたらしいです。しかしC言語派からは実用に耐えないなどの批判もされていたようです。

Pascalの軽い勉強法

どうせやるなら古典的教科書を一度読んでみようと決めました。しかしPascalの予習をしておいたほうが良さそうです。本を買って読んだり参考のWebサイトを探すのも手間なので、まずはChatGPT-4におすすめの課題を聞いてみました。

回答では10個の課題が得られました。Pascalの経験が無いので、一つづつプログラムを出力してもらい「写経」することにしました。今はとりあえず3番目まで終わらせました。軽く書いていますが、なれたC言語やPythonと勝手が違って面白いです。最初に変数を全て宣言したり、処理をbeginとendで囲んだりして風変わりな感じがします。

今後の予定

まずはChatGPT-4で出された課題10個の写経を終えたいと思います。その後、Pascalを作ったWirthの教科書へ進みたいと思います。

写真はPascalということでフランスで撮ったやつにしました。たしか軍事博物館だったと思います。ナポレオンがいかに凄いか、フランス人が自慢に思っているのかがよく分かりました。

それではまた。

参考:ChatGPT-4によるPascalを理解するための10の課題

Here are some tasks you can try to practice your Pascal programming skills:

1. Hello, User!: Write a program that prompts the user for their name and then prints a personalized greeting, like "Hello, [Name]!".

2. Arithmetic Calculator: Write a program that takes two numbers and an operator (+, -, *, /) as input from the user, performs the specified operation, and prints the result.

3. Factorial: Write a program that computes the factorial of a given positive integer using both iteration and recursion.

4. Fibonacci Sequence: Write a program that generates the first N Fibonacci numbers, where N is a positive integer provided by the user. Implement both iterative and recursive solutions.

5. Prime Numbers: Write a program that prints all prime numbers between 1 and a given positive integer N provided by the user.

6. Greatest Common Divisor (GCD): Write a program that calculates the GCD of two positive integers using Euclid's algorithm.

7. Array Sorting: Write a program that sorts an array of integers in ascending order using different sorting algorithms, such as Bubble Sort, Selection Sort, and Insertion Sort.

8. Palindrome Checker: Write a program that checks if a given word or phrase is a palindrome (reads the same backward and forward). The program should ignore spaces, punctuation, and case sensitivity.

9. Basic File Operations: Write a program that reads a text file and counts the number of lines, words, and characters in it. The program should also create a new file with the same content but all characters in uppercase.

10. Simple Address Book: Create a program that allows the user to manage an address book with basic operations like adding, deleting, and searching for contacts. You can store the contacts in a file or an in-memory data structure, such as an array or a linked list.

These tasks cover a range of programming concepts, from basic input/output and control structures to more advanced topics like recursion, file handling, and data structures. As you work through these tasks, you'll gain a better understanding of Pascal programming and improve your problem-solving skills.

ChatGPT-4, May 23 version


無料のプログラミングクラブCoderDojoを運営するにあたり寄付を受け付けています。お金は会場費・Wifiの費用・教科書に使用します。