1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

paiza.ioでelixir その236

Posted at

概要

paiza.ioでelixirやってみた。
練習問題、やってみた。

練習問題

hanoiモジュールをセーブして、ロードして実行せよ。

サンプルコード


File.write "H.exs", """
defmodule H do
    def hanoi(0, _, _) do
    end
    def hanoi(n, a, b) do
        hanoi n - 1, a, 6 - a - b
        IO.puts "円盤\#{n}を \#{a} -> \#{b}"
        hanoi n - 1, 6 - a - b, b
    end
end
"""
|> IO.inspect

File.ls!
|> IO.inspect

{:ok, code} = File.read("H.exs")
Code.eval_string(code)

H.hanoi 3, 1, 2
|> IO.inspect




実行結果

"defmodule H do\n    def hanoi(0, _, _) do\n    end\n    def hanoi(n, a, b) do\n        hanoi n - 1, a, 6 - a - b\n        IO.puts \"円盤\#{n}を \#{a} -> \#{b}\"\n        hanoi n - 1, 6 - a - b, b\n    end\nend\n"
["main.tar.gz", "Main.exs", "runner_mysql", "exec_stdin.txt", "run_user",
 "runner_php", "exec_stderr.txt", "H.exs", "exec_command", "exec_stdout.txt"]
2
円盤1を 1 -> 2
円盤2を 1 -> 3
円盤1を 2 -> 3
円盤3を 1 -> 2
円盤1を 3 -> 1
円盤2を 3 -> 2
円盤1を 1 -> 2

成果物

以上。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?