Quantcast
Channel: ocaml - gifnksmの雑多なメモ
Browsing latest articles
Browse All 20 View Live

KコンビネータとSコンビネータ

OCaml勉強中。KコンビネータとSコンビネータが出てきた。OCamlでfun式と関数適用の組み合わせ「のみ」で表現できる関数はSとKのを関数適用で組み合わせることですべて表現できることが知られています。# let s x y z = x z (y z);;val s : ('a ->'b ->'c)->('a ->'b)->'a ->'c...

View Article



引き続きOCaml

自然数rについて、x*x + y * y = rを満たす自然数(x, y)のリストを返す関数ただし、x ≧ y ≧ 0# let squares r =let isqrt n = int_of_float (sqrt (float_of_int n))inlet y_max = isqrt (r / 2)inletrec get_list y list=if y >= y_max...

View Article

多相的ヴァリアントとデータ構造とパターンマッチ

練習問題を解いたら結構長いコードになった。せっかくなので、記念カキコ。作って満足してしまった。動作確認はまた今度。(* 図形を定義 *)type figure =Point|Circleoffloat|Rectangleoffloat*float|Squareoffloat;;(* 座標情報をもったデータ *)type'a with_location ={loc_x : float; loc_y :...

View Article

Image may be NSFW.
Clik here to view.

再帰的データ型

さらに引き続きOCaml勉強中。この本を使って勉強しています。プログラミング in OCaml ~関数型プログラミングの基礎からGUI構築まで~作者:五十嵐淳出版社/メーカー:技術評論社発売日: 2007/11/29メディア:単行本(ソフトカバー)購入: 11人 クリック: 169回この商品を含むブログ (50件)...

View Article

再帰的データ型(二分木)

さっそくデータ型を定義。大きさと深さを求める関数も定義。# type'a tree =Lf|Brof'a *'a tree *'a tree;;type'a tree =Lf|Brof'a *'a tree *'a tree # letrec size =functionLf->0|Br(_, left, right)->1 + size left + size right;;val...

View Article


昨日の続き(二分木とバラの木)

木構造の定義# type'a tree =Lf|Brof'a *'a tree *'a tree;;type'a tree =Lf|Brof'a *'a tree *'a tree # type'a rosetree =RLf|RBrof'a *'a rosetree list;;type'a rosetree =RLf|RBrof'a *'a rosetree list # let rtree...

View Article

久々OCaml

引き続き練習問題。# type token =PCDATAofstring|Openofstring|Closeofstring;;type token =PCDATAofstring|Openofstring|Closeofstring # type('a, 'b) xml =XLfof'b option|XBrof'a *('a, 'b) xml list;;type('a, 'b) xml...

View Article

足し算・かけ算からなる数式を表す型

まずは型の定義。# type arith =Constofint|Addof arith * arith |Mulof arith * arith;;type arith =Constofint|Addof arith * arith |Mulof arith * arith 式を評価する関数・文字列に変換する関数・展開する関数# letrec eval =functionConst n ->...

View Article


無限数列

整数の無限数列のジェネレータみたいなもの。# type intseq=Consofint*(int-> intseq);;type intseq =Consofint*(int-> intseq)フィボナッチ数列のジェネレータ# let fib =letrec f x y =Cons(x, f (x+y))inCons(1, f 1);;実際に計算する関数# letrec nthseq...

View Article


素数の無限数列

# letrec prime_seq primes x =let x'= x + 1inletrec is_prime n =function[]->true| p :: rest -> p*p <= x'&& n mod p <>0&& is_prime n rest inif is_prime x' primes...

View Article

unit型を使った無限数列

# type'a seq =Consof'a *(unit->'a seq);;type'a seq =Consof'a *(unit->'a seq) # letrec from n =Cons(n, fun()-> from (n+1));;val from : int->int seq =<fun> # letrec take n (Cons(x,...

View Article

書き換え可能なデータ構造を使ったキュー

いつもの如くまずは型の定義。# type'a mlist =MNil|MConsof'a *'a mlist ref;;type'a mlist =MNil|MConsof'a *'a mlist ref # type'a queue ={mutable head : 'a mlist;mutable tail : 'a mlist};;type'a queue ={mutable head :...

View Article

任意精度の有理数計算によるNewton-Raphson法

# openNum;; # (* f'(x)を求める *)let deriv f =let dx =Int10**/ Int(-50)infun x ->(f (x +/ dx) -/ f x) // dx;;val deriv : (Num.num ->Num.num)->Num.num ->Num.num =<fun> # (* f(x), f(f(x)),...

View Article


二分探索木を用いたテーブルの定義

# module type TABLE=sigtype('a, 'b) t val empty : ('a, 'b) t val add : 'a ->'b ->('a, 'b) t ->('a, 'b) t val retrieve : 'a ->('a, 'b) t ->'b optionval dump : ('a, 'b) t ->('a...

View Article

Queueの二通りの定義の仕方

シグネチャの定義# module type QUEUE=sigtype'a t exceptionEmptyval empty: 'a t val add: 'a t ->'a ->'a t val take: 'a t ->'a *'a t val peek: 'a t ->'a end;;module type QUEUE=sigtype'a t...

View Article


Image may be NSFW.
Clik here to view.

円周率を求めてみる

なんとなくやりたくなったからやってみた。意外と素直にあっさり書けた。やっぱり有理数型があるとこういうの楽だなぁ。使ったアルゴリズムは、円周率 - Wikipediaで見つけたこの式。arcsin(x)のx=1/2におけるテイラー展開で、収束が速いらしい。へぇー。で、できたのがこれ。openNumlet pi length =let min =Int1 // Int10**/ Int length...

View Article

catコマンド

これもまた簡単に実装できた。入出力関係は素直に書こうとするとやっぱり手続き型言語みたいになっちゃうのかな。let version ="0.1"let display_linenum = ref falselet filenames = ref []let spec =[("-n", Arg.Set display_linenum, "Display line...

View Article


wcコマンド

let version ="0.1"let display_byte_count = ref falselet display_line_count = ref falselet display_word_count = ref falselet filenames = ref []let spec =[("-c", Arg.Set display_byte_count, "Display the...

View Article

foldコマンド

let version="0.1"let display_width = ref 80let filenames = ref []let spec =[("--width", Arg.Int(fun w -> display_width := w), "Set width of lines");("-version", Arg.Unit(fun()->Printf.printf...

View Article

ぐいぐい

コンパイルのやり方とか、ファンクターとか、オブジェクトとか、ラベル付き引数とか、オプション引数とか、多相ヴァリアントとかいろいろやったけど、全部すっ飛ばしていきなりGUI。お金を預けたり引き出したりする簡単なプログラム。openTkopenPrintf(* 残高操作 *)let balance = ref 0let add_balance x = balance :=!balance + x (*...

View Article
Browsing latest articles
Browse All 20 View Live




Latest Images