A language that doesn’t affect the way you think about programming is
not worth knowing. --- Alan Perlis
Erlang 虽然是门小众的语言,在并发方面十分出色,有其独特的哲学和语法,是一门很值得了解的语言。]
下面介绍一下 Erlang 比较有特点的地方。程序员
若是一个变量,没有值,则能够用来作赋值。好比 X = 1.
ruby
A = [1, 2, 3]. [H|T] = A. H = 1. T = [2, 3].
也能够用来作检查:并发
point % 以小写字母开头的是 atome,有些相似 ruby 的 symbol Point = {point, 10, 45}. {point, X, Y} = Point. % X = 10, Y = 45 {abc, X, Y} = Point. % ** exception error: no match of right hand side value {point,10,45} {point, Z, Z} = Point. % ** exception error: no match of right hand side value {point,10,45}
用来作 dispatch:ide
area({rectangle, Width, Ht}) -> Width * Ht; area({circle, R}) -> 3.14159 * R * R.
guards 相似 ruby 的 case,区别是能够用来定义方法,这样,一个方法就被分解成了不一样部分,使 Erlang 的方法变得更短。atom
max(X, Y) when X > Y -> X; max(X, Y) Y.
filter:spa
filter(P, [H|T]) -> case P(H) of true -> [H|filter(P, T)]; false -> filter(P, T) end; filter(P, []) -> [].
更短的 filter:code
filter(P, [H|T]) -> filter1(P(H), H, P, T); filter(P, []) -> []. filter1(true, H, P, T) -> [H|filter(P, T)]; filter1(false, H, P, T) -> filter(P, T)
sum([], N) -> N; sum([H|T], N) -> sum(T, H+N).
在 Erlang 中,递归被大量使用。
递归有两个好处,一个是能够用来分解问题,使问题变得很容易处理。
一个是,递归能够显示的维护状态,避免了赋值,维持了 immutable,方便维护和方便并发。递归
Erlang 很是独特,这些独特之处能够帮助、规范程序员分解问题,让 Erlang 的代码变得更好维护。ci