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