こちらの記事の続きです。
ChatGPT で elixir を学ぶ (その2)
掛け算九九の表を作成してみます。
残念ながら、ChatGPT も、Gemini も実行可能なプログラムは作成できませんでした。
Python のプログラム
multiplation.py
#! /usr/bin/python
#
import sys
#
sys.stderr.write("*** 開始 ***\n")
for it in range(1,10):
for jt in range(1,10):
kt = it * jt
print("%2d" % kt,end = " ")
print()
sys.stderr.write("*** 終了 ***\n")
実行結果
$ ./multiplation.py
*** 開始 ***
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
*** 終了 ***
ChatGPT が作成したプログラム
mch01.exs
IO.puts("*** 開始 ***")
for it <- 1..9 do
for jt <- 1..9 do
kt = it * jt
IO.write("#{kt} ")
end
IO.puts()
end
IO.puts("*** 終了 ***")
実行結果
$ elixir mch01.exs
*** 開始 ***
1 2 3 4 5 6 7 8 9 ** (UndefinedFunctionError) function IO.puts/0 is undefined or private. Did you mean:
* puts/1
* puts/2
(elixir 1.14.0) IO.puts()
mch01.exs:8: anonymous fn/2 in :elixir_compiler_0.__FILE__/1
(elixir 1.14.0) lib/enum.ex:4299: Enum.reduce_range/5
mch01.exs:3: (file)
Gemini が作成したプログラム
mge01.exs
defmodule Main do
def main do
IO.stderr.write("*** 開始 ***\n")
for it <- 1..9 do
for jt <- 1..9 do
kt <- it * jt
IO.write("%2d ", kt)
end
IO.puts("")
end
IO.stderr.write("*** 終了 ***\n")
end
end
Main.main
実行結果
$ elixir mge01.exs
warning: variable "kt" does not exist and is being expanded to "kt()", please use parentheses to remove the ambiguity or change the variable name
mge01.exs:7: Main.main/0
warning: variable "kt" does not exist and is being expanded to "kt()", please use parentheses to remove the ambiguity or change the variable name
mge01.exs:8: Main.main/0
warning: undefined function kt/0 (expected Main to define such a function or for it to be imported, but none are available)
mge01.exs:8
warning: undefined function kt/0 (expected Main to define such a function or for it to be imported, but none are available)
mge01.exs:7
** (CompileError) mge01.exs:7: undefined function <-/2 (expected Main to define such a function or for it to be imported, but none are available)
私が作成したプログラム
multiplation.exs
IO.puts :stderr,"*** 開始 ***"
for it <- 1..9 do
for jt <- 1..9 do
kt = it * jt
sx = Integer.to_string(kt)
:io.format("~2s ",[sx])
end
IO.puts ""
end
IO.puts :stderr,"*** 終了 ***"
実行結果
$ elixir multiplation.exs
*** 開始 ***
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
*** 終了 ***