160 likes | 262 Views
nML programming. applicative programming. value-oriented, not machine-oriented. 값만 생각하는 프로그래밍. 복잡한 머리는 터트려버려라. What is applicative programming?. VALUES ARE IMMUTABLE. 1, 2, 3, 1+2, {kwang, young}U{sang}. OBJECTS ARE CHANGING. 1, 2, 3, 1.add2, {kwang, young}.add(sang). S. S. 1, 2, 3, 4.
E N D
nML programming applicative programming value-oriented, not machine-oriented 값만 생각하는 프로그래밍 복잡한 머리는 터트려버려라
What is applicative programming? VALUES ARE IMMUTABLE. 1, 2, 3, 1+2, {kwang, young}U{sang} OBJECTS ARE CHANGING. 1, 2, 3, 1.add2, {kwang, young}.add(sang)
S S 1, 2, 3, 4 1, 2, 3 S.add(4) T = add(S,4) T 1, 2, 3 1, 2, 3 4 S S
Homework 2-1 ([],[]) fun insert(x,l) = x::l fun delete(x::[]) = x | delete(x::r) = delete r | delete [] = raise ... fun delete(x::r) = x | delete [] = raise ... ([1],[]) ([2,1],[]) ([3,2,1],[]) ([],[1,2,3]) ([], [2,3]) ([4], [2,3]) ([4],[3]) ([9,4],[3])
Homework 2-2 type val = type env = type mem = fun eval(SEQ(e1,e2),env,mem) = let val (v1,mem1) = eval(e1,env,mem) val (v2,mem2) = eval(e2,env,mem1) in (v2,mem2) end
Modules in nML 이 보따리 이름은 Box val x = … type t=A|B … structure Box = struct val x = … type t = … end Box.x … Box.A module(보따리)는 정의한(이름붙인) 것들을 하나로 모아놓고 이름붙여 놓은 것 입니다.
Modules in nML 그러한 보따리의 타입이 signature입니다. signature S = sig … end val x: int -> int type t val x: int -> int type t = A|B signature matching structure XX: S = struct … end val x: int -> int
Modules in nML function(함수)는 값을 받아서 값을 만드는 함수 fun f(x,y) = x+y functor(모듈함수)는 모듈을 받아서 모듈을 만드는 함수 functor F(X,Y) = struct … end functor F(X: sig … end, Y: sig … end) = struct … end functor F(X: S1, Y: S2) = struct … end
signature Animal = sig val age: int val think: string -> bool val feel: string -> bool end functor Couple(Beauty: Animal, Beast: Animal) = struct val age = Beauty.age + Beast.age fun think x = (Beauty.think x) orelse (Beast.think x) fun feel x = (Beauty.feel x) andalso (Beast.feel x) end
signature CAR = sig type speed type fuel val accelerator: int -> speed val break: int -> speed val fill_tank: int -> fuel end structure Porche = struct type speed = int type fuel = EMPTY | FULL of int fun accelerator n = n**n fun break n = n/10 fun fill_tank n = FULL n end
signature CAR = sig type speed type fuel val accelerator: int -> speed val break: int -> speed val fill_tank: int -> fuel end functor DriverSchool(Car: CAR) = struct fun speed_up n = Car.accelerator n fun slow_down n = Car.break n fun get_ready n = Car.fill_tank n end structure TicoDriver = DriverSchool(Tico) structure PorcheDriver = DriverSchool(Porche)
signature STACK = sig type atom type ‘a stack val empty_stack: atom stack val push: atom * atom stack -> atom stack end functor MakeStack(S: sig type t end) = struct type atom = S.t type ‘a stack = ‘a list val empty_stack = [] fun push (x, stk) = x::stk end
structure IntStk = MakeStack(struct type t = int end) structure StrStk = MakeStack(struct type t = string end) structure PairStk = MakeStack(struct type t = int * string end)