Download presentation
Presentation is loading. Please wait.
Published byEdward Paul Modified over 9 years ago
1
nML programming applicative programming value-oriented, not machine-oriented 값만 생각하는 프로그래밍 복잡한 머리는 터트려버려라
2
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)
3
1, 2, 3 1, 2, 3, 4 S S S.add(4) T = add(S,4) S T S 1, 2, 3 1, 2, 3 4
4
F-22 Standard ML/NJ
5
Rafale Ocaml
6
nML ?
7
Homework 2-1 ([],[]) ([1],[]) ([2,1],[]) ([3,2,1],[]) ([],[1,2,3]) fun insert(x,l) = x::l fun 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])
8
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
9
Modules in nML val x = … type t=A|B … 이 보따리 이름은 Box structure Box = struct val x = … type t = … end Box.x … Box.A module( 보따리 ) 는 정의한 ( 이름붙인 ) 것들을 하나로 모아놓고 이름붙여 놓은 것 입니다.
10
Modules in nML 그러한 보따리의 타입이 signature 입니다. val x: int -> int type t val x: int -> int type t = A|B val x: int -> int signature S = sig … end signature matching structure XX: S = struct … end
11
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
12
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
13
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
14
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
15
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
16
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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.