76. 8 门编程语言的设计思考
Object-oriented
Account = {}
f u n c t i o n Account : new ( )
o = { b a l a n c e = 0}
setmetatable (o , s e l f )
s e l f . __index = s e l f
return o
end
f u n c t i o n Account : d e p o s i t ( v )
s e l f . balance = s e l f . balance + v
end
B a n k P r e s i d e n t A c c o u n t = Account : new ( )
fu n c t i o n BankPresidentAccount : d ep o s i t ( v )
s e l f . balance = s e l f . balance + v * 2
end
j o e = B a n k P r e s i d e n t A c c o u n t : new ( )
joe : deposit (17)
print ( joe . balance )
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
94. 8 门编程语言的设计思考
Increment Operator
$a =3; $a++; p r i n t $a ;
$a= ’ ab ’ ; $a++; p r i n t $a ;
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
95. 8 门编程语言的设计思考
Increment Operator
$a =3; $a++; p r i n t $a ;
$a= ’ ab ’ ; $a++; p r i n t $a ;
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
96. 8 门编程语言的设计思考
Object-oriented
package P e r s o n ;
sub new
{
my $ c l a s s = s h i f t ;
my $ s e l f = { _ f i r s t N a m e => s h i f t , _lastName
bless $self , $class ;
$self ;
}
=> s h i f t
package Employee ;
use P e r s o n ;
o u r @ISA = qw( P e r s o n ) ;
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
119. 8 门编程语言的设计思考
Ruby 1.9 hash syntax
是不是类似 Smalltalk 的 keyword message
def f ( * a r g s )
p args
end
f 3 , ’ a ’ , one : 5 , two : 8
=> [3, "a", {:one=>5, :two=>8}]
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
120. 8 门编程语言的设计思考
Ruby 1.9 hash syntax
是不是类似 Smalltalk 的 keyword message
def f ( * a r g s )
p args
end
f 3 , ’ a ’ , one : 5 , two : 8
=> [3, "a", {:one=>5, :two=>8}]
{:arr => [1,2], :str => ‘hello’}
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
121. 8 门编程语言的设计思考
Block
是不是 Python with statement 的通用版
def f u n &b l o c k
# acquire resource
b l o c k . c a l l 42
# release resource
end
f u n do
p ’ hello ’
end
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
122. 8 门编程语言的设计思考
Block
是不是 Python with statement 的通用版
def f u n &b l o c k
# acquire resource
b l o c k . c a l l 42
# release resource
end
f u n do
p ’ hello ’
end
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
166. 8 门编程语言的设计思考
Currying
# let add x y = x + y;;
val add : int -> int -> int = <fun>
# add 3;;
- : int -> int = <fun>
Ruby/CoffeeScript 也用了 juxtaposition,但是若支持
currying,由于 dynamic typing,容易产生 bug
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
167. 8 门编程语言的设计思考
Module system
把相互关联的函数组织在一起 (如定义了一个数据类
型以及操作这个数据类型的一系列函数)
module S t a c k = s t r u c t
exception Stack_is_empty
type ’ a s t a c k = Empty | Elem o f ’ a * ’ a s t a c k
l e t push s e = Elem ( e , s )
l e t pop = f u n c t i o n
| Empty −> r a i s e S t a c k _ i s _ e m p t y
| Elem (_ , s ) −> s
l e t top = function
| Empty −> r a i s e S t a c k _ i s _ e m p t y
| Elem ( e , _) −> e
end
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
168. 8 门编程语言的设计思考
Module system
把相互关联的函数组织在一起 (如定义了一个数据类
型以及操作这个数据类型的一系列函数)
separate compilation
module S t a c k = s t r u c t
exception Stack_is_empty
type ’ a s t a c k = Empty | Elem o f ’ a * ’ a s t a c k
l e t push s e = Elem ( e , s )
l e t pop = f u n c t i o n
| Empty −> r a i s e S t a c k _ i s _ e m p t y
| Elem (_ , s ) −> s
l e t top = function
| Empty −> r a i s e S t a c k _ i s _ e m p t y
| Elem ( e , _) −> e
end
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
169. 8 门编程语言的设计思考
Functor
module type Eq = s i g
type t
v a l eq : t −> t −> b o o l
end
module MakeSet ( E : Eq ) = s t r u c t
type t = E . t
l e t empty = [ ]
l e t add x s = x : : s
l e t f i n d x s = L i s t . f i n d ( E . eq x ) s
end
module S t r E q = s t r u c t
type t = s t r i n g
l e t eq s 1 s 2 = s 1 = s 2 ;
end
module S t r S e t = MakeSet ( S t r E q )
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
170. 8 门编程语言的设计思考
Encapsulation
# let o = object
v a l xs = ” ray ”
method o u t p u t = p r i n t _ e n d l i n e x s
end ; ;
v a l o : < o u t p u t : u n i t > = <o b j >
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
171. 8 门编程语言的设计思考
Row polymorphism
# fun x −> x#o u t p u t ; ;
− : < o u t p u t : ’ a ; . . > −> ’ a = <fun>
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
172. 8 门编程语言的设计思考
Private method
single coersion
l e t f = fun x −> x#o u t p u t ; ; end
type ’ a y = <o u t p u t : ’ a ; i n p u t : ’ a −> u n i t > −> ’ a
f :> ’ a y
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
191. 8 门编程语言的设计思考
Num [a]
.
Example (Example)
.
i n s t a n c e Num a = Num [ a ] where
>
( f : f s ) + ( g : g s ) = f+g : f s+g s
fs + [] = fs
[ ] + gs = gs
( f : f s ) * ( g : g s ) = f *g : [ f ] * g s + f s * ( g : g s )
_ * _ = []
[ 1 , 1 ] ^ 4 == [ 1 , 4 , 6 , 4 , 1 ]
.
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
192. 8 门编程语言的设计思考
Re-invent imperative programming
do x <− g e t ; p u t 5 ; y <− g e t ; p u t 6
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
193. 8 门编程语言的设计思考
Parsec
解析一个 csv 文件
.
Example (csv)
.
csv : : CharParser st [ [ String ] ]
c s v = ( r e c o r d ‘ sepEndBy ‘ n e w l i n e ) <* e o f
where
r e c o r d = ( q u o t e d <|> many ( noneOf ” , ” n ” ) ) ‘ sepBy ‘
q u o t e d = between ( c h a r ’ ” ’ ) ␣ ( c h a r ␣ ’ ” ’ ) . many $ none
− 可以自定义操作符,甚至优先级
.
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
194. 8 门编程语言的设计思考
Pattern matching
.
Example (Pattern guards)
.
add x y | 3 <− x = x + y
.
.
Example (View patterns)
.
m y s o r t ( s o r t −> x s ) = x s
.
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
213. 8 门编程语言的设计思考
Failure
不要吐槽这段代码不 idiomatic,照顾不用 Ruby 的同学
def f o o
return
return
return
return
end
’ Meowth ’ u n l e s s f 36
’ Persian ’ unless g 1
’ f q j ’ u n l e s s h 22
17
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
214. 8 门编程语言的设计思考
Error monad
f o o = do
f 36
g 1
h 22
r e t u r n 17
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
217. 8 门编程语言的设计思考
Environment
def
def
def
def
f (x );
g(x );
h(x );
i (x );
g ( x ) ; i ( x ) ; end
h ( x ) ; end
x * x ; end
x +3; end
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.
218. 8 门编程语言的设计思考
Reader monad
f
g
h
i
=
=
=
=
g >> i
h
j o i n ( * ) <$> a s k
(+3) <$> a s k
..
.
..
.
..
.
. . . . . . . . . . . .
.. .. .. .. .. .. .. .. .. .. .. .. ..
.
..
.
..
.
..
.
..
.