Skip to content

Instantly share code, notes, and snippets.

@kjunichi
Last active February 26, 2018 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjunichi/5641297 to your computer and use it in GitHub Desktop.
Save kjunichi/5641297 to your computer and use it in GitHub Desktop.

多倍長

#require "num";;
Big_int.big_int_of_string "1234567890000000000000000000000000000000000000000000000000000000000000000000000001";;

多次元配列

let imageBuf p n m init =
	let result = Array.make p (Array.make n (Array.make m init)) in
  		for j = 1 to p - 1 do
  			result.(j) <- Array.make n (Array.make m init);
    		for i = 1 to n - 1 do
      			result.(j).(i) <- Array.make m init
    		done;
    	done;
    	result;;

let buf = imageBuf 3 200 200 0.0;;
buf.(0).(1).(1) <- 0.1;
buf.(1).(1).(1) <- 0.2;
print_float buf.(0).(1).(1);;
print_endline " <- buf.(0).(1).(1)";;
print_float buf.(1).(1).(1);;
print_endline " <- buf.(1).(1).(1)";;

文法

If文

if hoge then begin foo; bar; end else hoge;fuga; end;;

let a = 3;;

print_int (Random.int 10);;

if a > Random.int 10 then
begin
 print_endline "hoge";
 print_int (Random.int 10)
end
else
begin
let b = a in
print_int b;
end
;;

OpenGL

ocamlopt -I ~/.opam/4.01.0/lib/lablgl lablgl.cmxa lablglut.cmxa fractal.ml -o fractal
eval `opam config env`

を.bashrcに記述したら、ocamlfindが~/.opam配下を見るようになった。

ocamlfind ocamlopt -o fractal -linkpkg -package lablgl,lablgl.glut fractal.ml

図形描画

OSXだとX11つかっていた

#load "graphics.cma" ;;
Graphics.open_graph "" ; (* Caml graphicsウィンドウを開く *)

Graphics.plot 128 128;


きっかけ

関数型、だいぶ前から流行りそうと知っていたが、今年になって気が向いた。 Haskellも並行していじってみようと思っているが、なんとなくフランス生まれとかオシャレ系な気がして。。 あと、ライブラリが充実しているとの記述を見つけたり、で

DBを扱ってみたい

PostgreSQLしかプライベートは使う気がないのだが、やはり、ライブラリが見つかり、これからはじめるとこ

圏論とかで、もう12億URLくらいクロールした独自のWeb検索システムのデータを取り扱ってみたい。

できた!

準備

#use "topfind";;
#thread;;
#require "postgresql";;

接続

Open Postgresql;;
let c1 = new connection ~dbname:"websearch" ~host:"192.168.0.3" ~user:"junichi" ();;

()が重要、これ付けないと、エラーにならずに動かない。

参考

Re: Programming challenge: Query a database

open Postgresql;;
let conn = new connection ~host:"localhost" ~dbname:"linux" ~user:"test" ~password:"test123" ();;
let query = "SELECT name,distro FROM userinfo WHERE gui = $1 ORDER BY name";;
let rs = conn#exec ~expect:[Tuples_ok] ~params:[|Sys.argv.(1)|] query;;
conn#finish;;
Printf.printf "Number of matches: %d\n%-20s| %s\n%s\n" rs#ntuples "User" "Distro" (String.make 35 '-');;
for i = 0 to rs#ntuples - 1 do
   let row = rs#get_tuple i in
   Printf.printf "%-20s| %s\n" row.(0) row.(1);
done;;

カレントディレクトリを取得するには

Sys.getcwd();;
sudo apt-get install libpostgresql-ocaml

学んだこと

変数への破壊的代入

これと反対なのが、 「変数の値をあとから変更しない」(単一代入)

末尾再帰

「後にする処理」がない関数呼び出しのことを「末尾呼び出し」という。特に、すべての再帰呼び出しが末尾呼び出しであるような再帰関数のことを「末尾再帰関数」という。

処理の高速化にもつながる!?

関数が返ってきた後にする処理

破壊的代入を使用しないでループと同様の計算を表現できる

パッケージの使い方

#use "topfind";;

拡張子

.cma

#load "hoge.cma";;

パッケージシステムいろいろ

GODI

OASIS

OPAM

2017年現在このOPAMを使用中

参考リンク

エラーこれくしょん

No implementations provided for the following modules

関連記事

関連投稿

Link

関連

アクセス解析タグ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment