LoginSignup
1
0

More than 1 year has passed since last update.

paiza.ioでelixir その68

Posted at

概要

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

練習問題

通帳に300円あります。
1000円、入金して、500円、払い出しました。

サンプルコード


defmodule Account2 do
	defstruct balance: 0
	def withdraw(account, delta) do
		%Account2{ balance: account.balance - delta }
	end
	def deposit(account, delta) do
		%Account2{ balance: account.balance + delta }
	end
end

defmodule Main do
	require Account2
    my = %Account2{balance: 300}
    |> IO.inspect
    my = Account2.deposit(my, 1000)
    |> IO.inspect
    my = Account2.withdraw(my, 500)
    |> IO.inspect
end


実行結果

%Account2{balance: 300}
%Account2{balance: 1300}
%Account2{balance: 800}

成果物

以上。

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