OCaml 2009.. (khheo@ropas.snu.ac.kr) 1
ML 2
ML OCaml INRIA, France SML Bell lab. & Princeton, USA nml SNU/KAIST, KOREA 3
4
(let) (* ex1.ml *) let a = 10 let add x y = x + y (* ex2.ml *) let sumofsquare x y = let square x = x * x in square x + square y let sumofsquare x y = let sqx = x * x in let sqy = y * y in sqx + sqy 5
(if, match) (* ex3.ml *) let iseven n = if n mod 2 = 0 then true else false (* ex4.ml *) let iseven n = match n mod 2 with 0 -> true 1 -> false (* ex5.ml *) let is3mulitple n = match n mod 3 with 0 -> true _ -> false 6
let iseven n = if n mod 2 = 0 then true else false let rec fac n = if n = 0 then 1 else n * fac(n-1) : fun sigma (a, b, (fun x -> x + 1)) : function sigma (a, b, (function x -> x + 1)) 7
(int, string ), list, tuple, record 8
2 + 2.5 (X) (float_of_int 2) +. 2.5 (O) 9
) if, match (* C *) int lucky(){ int r; while (1){ r = rand() % 100; if (r == 50) return r; else printf( again\n ) } } (* OCaml *) let rec lucky () = let r = Random.int 100 in if r = 50 then r else (printf( again\n ); lucky ()) 10
int, float, string, char, unit list, tuple, record 11
[1;2;3] = 1::[2;3] = 1::2::[3] [ a ] = a ::[] let ilist1 = [1; 2; 3] let ilist2 = 4::ilist1 let ilist3 = ilist1@ilist2 let gethead l = match l with h::t -> h [] -> raise Error 12
let man = ( age, 24) let input = (10, 100, (fun x -> x*x)) let getfirst l = match l with (f, _, _) -> f 13
C let getname r = type subject = {name : string; credit : int} let subject = {name = PL ; credit = 3} match r with {name= n; credit = c} -> n 14
type value = int type tree = Leaf Node of value * tree * tree let t = Node (5, Leaf, Node (4, Leaf, Leaf)) let rec sum t = match t with Node (v, t1, t2) -> v + sum t1 + sum t2 Leaf -> 0 15
(Polymorphic type) type itree = Leaf of int Node of int * itree * itree type a tree = Leaf of a Node of a * a tree * a tree let a = Leaf 5 let b = Node ( b, Leaf l, Leaf r ) 16
17
ML h::t, (a, b, _), _ type exp = Num of int Add of exp * exp Minus of exp * exp Mult of exp * exp Div of exp * exp let rec eval exp = match exp with Num i ->... Add (e1, e2) ->... Minus (e1, e2) ->...... 18
(Polymorphic) let identity x = x let trans t = match t with (a, b) -> (b, a) let gethead l = match l with h::t -> h [] -> raise Error 19
let add x y = x + y let incr = add 1 let seven = incr 6 let incrn n = (fun x -> x + n) let rec map f l = match l with h::t -> (f h)::(map f t) [] -> [] let incrlist = map (fun x -> x + 1) [1; 2; 3; 4] 20
, exception Error of string let rec fac n = if n < 0 then raise (Error invalid arg ) else if n = 0 then 0 else n + fac (n - 1) let f n = let a = try fac n with Error s -> print_endline s; -1 _ -> print_endline unknown exception ; -1 in print_int a; 21
let count = ref 0 let acc n = count :=!count + 1 22
,,, : :, module type StackSig = sig! type 'a stack! exception StackEmpty! val emptystack : 'a stack! val push : 'a stack * 'a -> 'a stack! val pop : 'a stack -> 'a * 'a stack end module Stack : StackSig = struct! type 'a stack = 'a list! exception StackEmpty! let emptystack = []! let push (stk, itm) = itm::stk! let pop stk =!! match stk with!! (itm::stack) -> (itm, stack)!! [] -> raise StackEmpty end 23
(functor) (* set.ml *) module type OrderedType = sig type t val compare : t -> t-> int end module type Make (Ord : OrderedType) struct type elt = Ord.t type t = Empty Node of t * elt * t * int... end module OrderedString struct type t = string let compare = compare end module StringSet = Set.Make (OrderedString) module IntSet = Set.Make(module struct type t = int let compare = compare end) 24
http://caml.inria.fr/pub/docs/manual-ocaml/ libref/ Ocaml List 25
let x = [1;2;3] List.length x (* 3 *) List.hd x (* 1 *) List.tl x (* [2;3] *) List.rev x (* [3;2;1] *) 26
let x = [1;2;3] let iseven = fun y -> (y mod 2) = 0 List.for_all iseven x (* false *) List.exist iseven x (* true *) List.mem 2 x (* true *) List.filter (fun x -> x < 2) x (* [1] *) 27
let x = [1;2;3] List.iter print_int x (* unit *) List.map string_of_int x (* [ 1 ; 2 ; 3 ] *) List.fold_left (fun x y -> x*10+y) 0 x (* 123 *) List.fold_right (fun x y -> y*10+x) x (* 321 *) 28
29
30
, 4:00 ~ 5:30 : khheo@ropas.snu.ac.kr : hjkim@ropas.snu.ac.kr 31
(ocaml) 32
/ ocamlc 10%, 10% 33
nml http://ropas.snu.ac.kr/~ta/4190.310/09/nmlguide.ppt OCaml http://www.ocaml-tutorial.org/ OCaml http://caml.inria.fr/pub/docs/manual-ocaml/libref/ 34