関数型言語
折角のハイブリッド言語なのに関数型言語をよく知らないので、Genericsが使えるC#1.0程度でしか使えていない。なので、ちょっとStandard MLを勉強することにしました。で、今日の成果。
using System; def fibonacci (n) { def fib (n, b, a) { | (0, b, _) => b; | (n, b, a) => fib (n - 1, b + a, b); } fib (n, 1, 0); } Console.WriteLine (fibonacci (10));
以前よりは関数型言語っぽく書けた?(^^;
(追記) もっとListを使っていかないと駄目かな。
using System; using Nemerle.Collections; def map (f, l) { | (_, ) => | (f, x::xs) => f(x) :: map(f, xs) } def iter (f, l) { | (_, ) => () | (f, x::xs) => f(x); iter (f, xs) } def lx = [1,2,3,4,5]; def ly = map (fun (x) { x * x }, lx); iter (fun (x) { Console.Write ($"$x ") }, ly); /* 1 4 9 16 25 */