勉強のためにPerlでモナドを実装してみました。 免責 圏論のモナドの概念をPerlでシミュレートしようと言うエントリであって、モナドを開発にどう利用するかなどについてはびっくりするくらい言及しませんので悪しからず。 1. 圏を考える Perlのスカラ値の任意の集合を対象として、引数も戻り値も一つで副作用がないサブルーチンを射とします*1。domとcodについては、関数が正常に動作する範囲で適当にとりましょう。 * 例: 対象「文字列」から対象「整数」への射 length 2. 自己関手の表現 自己関手は、二つの写像からなります。 対象を対象に移す写像 T_object モノとしては考えますけど今回は実装しません*2。 射を射に移す写像 T_arrow 『サブルーチンを受け取ってサブルーチンを返すサブルーチン』として実装できます。 3. 自然変換の表現 関手Tから関手Sの自然変換は、対象A