Objective Programming Paradigm のサンプルコードをRubyで実装?
=begin | |
Class Factorial | |
# Fun: メンバ函数/メソッド定義? | |
Fun x | |
(* ! a b : 変数名 a に 値b が束縛される? | |
n 1 : 内部的にはリストの内部に連結されると想像 | |
+ n 1 : + オブジェクトはレシーバのリストをパーズして加算した値を返す? | |
*) | |
! n + n 1 | |
Print n | |
(* この条件判断はこれでいいの? *) | |
If = x 1 | |
Return 1 | |
End | |
Else | |
(* x を 2回呼んでいいの? *) | |
Return * x Fuctorial - x 1 | |
End | |
End | |
# Var: メンバ変数/プロパティ定義? | |
Var | |
n | |
End | |
Def | |
Init | |
! n 0 | |
End | |
End | |
End | |
Main Print Funtorial 100 | |
=end | |
def Fuctorial n | |
Fuctorial.new(n).x | |
end | |
class Fuctorial | |
def x | |
@n -= 1 | |
# puts n | |
if n <= 1 | |
@n = @s # 末尾なので状態を初期化時の値に戻しておく | |
1 | |
else | |
@n * x | |
end | |
end | |
# Var とは意味が違ふけど、便宜上。 | |
attr_accessor: n | |
def initialize(n) | |
@s = n | |
@n = n | |
end | |
end | |
puts Fuctorial 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment