2009-01-01から1ヶ月間の記事一覧

labelsとfletの違い

labelsは主に関数定義を入れ子にしたい場合に用いますが、fletというものもあるみたい。 外側(defun)と内側(labels,flet)の関数名が同一であった場合、内側の関数で再帰呼び出しをすると果たして誰にお呼びがかかるのか。labelsとfletはその時の振る舞いが異…

例外

(* 適当に例外を3つ宣言。楽でいいですね。 *) # exception E1;; exception E1 # exception E2;; exception E2 # exception E3;; exception E3 (* 例外を発生させるだけの子。 *) # let foo = function | 1 -> raise E1 | 2 -> raise E2 | 3 -> raise E3 | o…

レコード型

なんでOCamlのレコード型のフィールド名はみんな同じ空の下で管理されるんだろう。 # type a = { id : string; name : string };; type a = { id : string; name : string; } # type b = { id : string; value : int };; type b = { id : string; value : in…

パターンマッチによる再帰

CL-USER> (defmethod my-fold (f e (ls (eql nil))) e) CL-USER> (defmethod my-fold (f e ls) (my-fold f (funcall f e (car ls)) (cdr ls)) ) CL-USER> (my-fold #'(lambda (x y) (cons y x)) '() '(5 4 3 2 1)) (1 2 3 4 5) 思いつきでmultimethodsでやっ…