こんにちは tukinyanJP です。今回は、独自パラダイムを一人で考えてみました。多分、興味深い。なんといっても私は自称言語オタクですからね。
せっかくだったので、「逆算型」パラダイムを採用した計算用プログラミング言語、 Whatis? を作ってみました。
「逆算型」とは?
「逆算型」というのは、文字通り、逆算をします。例えば、フィボナッチ数列を求める関数「fib(n)」があったとします。普通の言語 Ruby / Python / Java / Perl では、 fib(任意の数) はいくつになるか? のような求め方しかできませんが、「逆算型」では、 fib(n) の解が 55 になる場合のとき、 n は何になるか? を求めることができます。
つまり、求めるような式を書かなくても、 「逆算型」では、一定の決まりを書くことで、どのような場合の解も見つけられるため、コードを書き出す時間が減ります。
以下に Whatis? の例を書きます。
Suppose Fact of N is 1 when N is equal to 0 # 訳: Fact を定義する。引数 N が 0 である場合は、1を返す。
Otherwise, Fact of N is N multiplied_by (Fact of (N minus 1)) # 訳: そうでなければ、N に Fact(N-1) をかけてこの解を返す。
What is Fact of 5? # => Fact of 5 is 120
What is N when Fact of N is 120? # => N is 5
Suppose Fib of N is N when N is equal to 0 # => これらは fib を求めるもの
Suppose Fib of N is N when N is equal to 1
Otherwise, Fib of N is (Fib of (N minus 1)) plus (Fib of (N minus 2))
What is Fib of 10? # => Fib of 10 is 55
What is N when Fib of N is 55? # => N is 10
のようになります。1
なんで作ろうと思ったか
ここ最近から、言語開発においてとても違和感を持っていました。「 本当の簡潔さとはなんだろう? 」と。確かに、可読性が高いのは正しいと思う。でも、可読性が高いからと言って、コードの記述量が多いと違うのではないだろうか? そう思い、「逆算型」を作ってみました。
終わりに
久々の投稿でした。最後まで読んでいただきありがとうございます。それでは、次の記事まで、さようなら。
-
ちなみにこのパラダイムのポテンシャルはとてもすごいです。
Suppose Power of Base and Exponent is 1 when Exponent is equal to 0 Otherwise, Power of Base and Exponent is Base multiplied_by (Power of Base and (Exponent minus 1)) What is Power of 2 and 3? What is Exponent when Power of 2 and Exponent is 8? Suppose Cmp of Principal and Years is Principal when Years is equal to 0 Otherwise, Cmp of Principal and Years is Cmp of ((Principal multiplied_by 103) divided_by 100) and (Years minus 1) What is Cmp of 1000000 and 24? What is Years when Cmp of 1000000 and Years is 2032775べき乗を逆算することで、対数をすることも可能ですし、複利のような複雑な計算も簡単にできます。 ↩