Nemerle

関数型言語への道

Haskellのチュートリアルをやっているのですが理解度チェックのため、練習問題をHaskell、Nemerle両方で解きながら進めてます。 using System; variant List [T] { | Nil | Cons { value : T ; next : List [T]; } } def show (l) { def iter (_) { | List.N…

クラス生成マクロ

マクロの練習。 using Nemerle.Compiler; using Nemerle.Collections; macro MakeClass (class_name : string) { // 現在のコンテキストを取得 def ctx = Nemerle.Macros.ImplicitCTX (); // 文字列を識別子に変換 def nm = Macros.UseSiteSymbol (class_nam…

インデントNemerle

インデントを使った文法がサポートされた模様。 using System; set class Math Add (x : int, y : int) : int x + y Sub (x : int, y : int) : int x - y static Main () : void def m = Math () def x = m.Add (1, 2) def y = m.Sub (3, 4) if (x < y) Cons…

Proxyマクロ

Nemerleのサンプルコードを漁っていたら、ちょっと面白いものを発見。 using System; public interface ICalc1 { Add (x : int, y : int) : int; Sub (x : int, y : int) : int; } public interface ICalc2 { Mul (x : int, y : int) : int; Div (x : int, y…

匿名メソッドの実装

delegateと通常メソッドの比較は過去に取り上げましたが、今回は、それが目的ではありません。 using System; delegate bool F(int x, int y); static class Program { static bool less (int x, int y) { return x < y; } public static void Main () { Dat…

インタプリタ

Nemerleにはnemishというインタプリタがあるのですが、ネタにするのをすっかり忘れてました。(^^; で、nemishを使う前に注意。nemish.exeのあるディレクトリにNemerle.Macros.dllをコピーする必要があります。GACに登録されているのに、何故か要求されるんで…

Partial applicationその4

部分適用は演算子にも使えたんですね。 using System; def lst = [2, 3, 4]; // 匿名関数 Console.WriteLine (lst.FoldRight (1, fun (x, y) { x + y })); Console.WriteLine (lst.FoldRight (1, fun (x, y) { x * y })); // 部分適用で同等の内容 Console.W…

MonoでSystem.Query

Sequence.csをgmcsコンパイル出来たので、System.QueryをNemerleから使ってみる。 using System.Console; using System.Query.Sequence; // def src = array [1, 2, 3, 4, 5, 6, 7, 8, 9]; // error! def src = [1, 2, 3, 4, 5, 6, 7, 8, 9]; def os = Order…

Lambda expressions

C#3.0のLambdaがシンプルで良い感じなので、Nemerleでも真似出来ないかと色々試すも、私の知識じゃ歯が立たず。あんまマクロ分かってないんですよね。(^^;で、格闘の跡。 using Nemerle.Compiler; // 引数1つ macro @fx (parm : parameter, body) syntax ("…

似非Restriction Operators

・The LINQ Project C#3.0とLINQが発表され、各地で盛り上がっているよーですが、仕事で嵌っていて全く情報が集められてません。悔しいのでNemerleで対抗です。よーわからないけど見様見真似。(^^; using Nemerle.Utility; [Record] class Person { [Accesso…

はじめての型推論

ひょっとしたら型推論をサポートする言語を使っていないと、ピンとこないかも知れないので、今更ながらご紹介。次のような、変数x,yを足す関数を定義し、コンパイルします。 def add (x, y) { x + y } すると、当然のごとく、 app.n:2:5:2:10: error: typing…

ジェネリックパラメータの明示的指定

Nemerleには型推論があるため、ジェネリック型を明示的に指定する必要がありませんが、実は明示的に指定する方法もありませんでした。最近やっと可能になったのですが、文法的にちょっとクセがあります。 using Nemerle.IO; class A [X] { public mutable a …

Partial applicationその3

更に色々使ってみる。 def func (x : int, y : string, z : double) : void { System.Console.WriteLine ($"$x,$y,$z"); } // 引数となるタプル def t1 = (1, "two", 3.0); def t2 = (2, 4.0); def t3 = (3, "four"); func (t1); func (_, "three", _) (t2);…

Partial application

日本語では部分適用と呼ぶようですが、関数型言語をやってないと馴染みがないですね。誤解を恐れずに動きだけを言うと、N個の引数を取る関数にM(M def add (x : int, y : int) { x + y; } def add7 = add (_, 7); /* // これと同じ def add7 = fun (x) { add…

Partial applicationその2

引数だけでなく、オブジェクトにも適用出来たりします。 class X { msg = null; public this (msg : string) { this.msg = msg; } public Say (key : string) : void { System.Console.WriteLine ($"$msg, $key"); } } def x1 = X ("Hello"); def x2 = X ("G…

Code Completion

・http://nemerle.org/Code_Completion 最近の目玉機能。ソースコードを食べてTypeTreeを作成します。自前でParserとかこさえる必要が無くなるので、ツール開発者には朗報かも。上記リンクを読むと雰囲気が掴めると思います。ただ、現在開発中の為、サンプル…

.NET2.0対応版

現在、Nemerleプロジェクトは.NET2.0のGenerics対応がメインになっているようなので、そっちを追っかけてみることに。ところが、このGenerics版は.NET2.0β2にバグがあってSVNからチェックアウトしたMonoでないとビルド出来ません。そのMonoもcscだとWin32Exc…

関数型言語

折角のハイブリッド言語なのに関数型言語をよく知らないので、Genericsが使えるC#1.0程度でしか使えていない。なので、ちょっとStandard MLを勉強することにしました。で、今日の成果。 using System; def fibonacci (n) { def fib (n, b, a) { | (0, b, _) …

遅延評価と無限リスト

遅延評価で遊んでみる。 using System; using Nemerle; #if false // 遅延評価を使わない場合 class Range { public Val : int; public Next : Range; public this (begin : int, step : int) { Val = begin; // コンストラクタが再帰的に呼ばれ、最終的には…

トップレベルに式を書く

と、マクロが勝手にmodule及びMainを用意して、その中に式を放り込んでくれます。 using System; def range (begin, end, step) { mutable n = begin; fun () { when (n >= end) throw InvalidOperationException (); def x = n; n += step; x; } } def time…

続return/break

http://d.hatena.ne.jp/akiramei/20050523#p1は、blockキーワードを使わないようになったようです。(リンク先の案が採用)

return/break

投票の結果が反映され、return/breakに相当するものがサポートされました。 using System; using Nemerle.Block; module M { Equals (lhs : array [int], rhs : array [int]) : bool { block (return) { when (lhs.Length != rhs.Length) return (false); fo…

Language Poll II

id:atsushienoさんところで紹介されていましたが、公式ページでNemerleについての投票をやってます。結果はこちら。 1. Which of the following features would you like to be added first to Nemerle. You can mark more than one, but the less you mark,…

Iterator

Don Box's Spoutletより http://pluralsight.com/blogs/dbox/archive/2005/04/17/7467.aspx あ〜、なるほど、匿名メソッドを使ってIteratorモドキが出来ますね。気づかなかった・・・(^^; using System; module M { fib () : void -> long { mutable a = 0L;…

lispモドキ

using System; module M { Main () : void { def car (ls) { match (ls) { | x :: _ => x; | _ => null; } }; def cdr (ls) { match (ls) { | _ :: xs => xs; | _ => } }; def ls = [10, "Hello", 30, "World", 50 : object]; Console.WriteLine (ls); mutab…

OracleClientを使う

Testing System.Data.OracleClientをNemerleらしさを強めに書き直してみる。 using System.Console; using System.Data; using System.Data.OracleClient; module M { dbstr = "Data Source=testdb;User Id=scott;Password=tiger;"; Main () : void { def it…

ループ処理

これを書こうとして悩む。(^^; for (int i = 0; i < n; ++i) { if (lhs [i] != rhs [i]) return i; } return n; Nemerleにはbreakもreturnもgotoも無かった。結局、 def iter (i, n) { if (i >= n || lhs [i] != rhs [i]) i; else iter (i + 1, n); }; iter …

すっかりVS.NETを使わなくなったので、GUIプログラミングもWinForms以外でやってみようか、GTK#に手を出してみることにしました。GTK#のチュートリアルみたいなのが見つからなかったので、GTK+ 2.0 チュートリアルを読みながら試行錯誤中。「GTK 版の Hello …

GenericsとMacro

過去に散々やったネタですが、 using System.Console; module Program { static Add ['t] (x : 't, y : 't) : 't { x + y; } static Main () : void { def x = 10; def y = 20; def a = "Hello, "; def b = "World."; WriteLine (Add (x, y)); WriteLine (Ad…

msi版バイナリ

が、壊れている気がします。マクロの展開が上手く出来ず、forループすら書けません。なので、svnからチェックアウトしてmakeすることをお勧めします。