nML programming applicative programming value-oriented, not machine-oriented 값값 값값값값 값값값값값 값값값 값값값 값값값값값값
Dec 31, 2015
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)
Homework 2-1
([],[])
([1],[])
([2,1],[])
([3,2,1],[])
([],[1,2,3])
fun insert(x,l) = x::lfun delete(x::[]) = x | delete(x::r) = delete r | delete [] = raise ...
fun delete(x::r) = x | delete [] = raise ...
([], [2,3])
([4], [2,3])
([4],[3])
([9,4],[3])
Homework 2-2type 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
val x = …type t=A|B…
이 보따리 이름은 Box
structure Box = struct val x = … type t = … end
Box.x … Box.A
module( 보따리 ) 는 정의한 ( 이름붙인 ) 것들을 하나로모아놓고 이름붙여 놓은 것 입니다 .
Modules in nML
그러한 보따리의 타입이 signature 입니다 .
val x: int -> inttype t
val x: int -> inttype t = A|B
val x: int -> int
signature S = sig … end
signature matching
structure XX: S = struct … end
functor( 모듈함수 ) 는 모듈을 받아서 모듈을 만드는 함수 functor F(X,Y) = struct … end
function( 함수 ) 는 값을 받아서 값을 만드는 함수 fun f(x,y) = x+y
Modules in nML
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.agefun 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
structure TicoDriver = DriverSchool(Tico)structure PorcheDriver = DriverSchool(Porche)
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
signature CAR = sig type speed type fuel val accelerator: int -> speed val break: int -> speed val fill_tank: int -> fuel end
signature STACK = sig type atom type ‘a stack val empty_stack: atom stack val push: atom * atom stack -> atom stackend
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