21. (Wikipedia)
In computer science, functional
programming is a programming paradigm
that treats computation as the evaluation
of mathematical functions and avoids state
and mutable data. It emphasizes the
application of functions, in contrast to the
imperative programming style, which
emphasizes changes in state.
Sunday, April 15, 12
22. (Wikipedia)
In computer science, functional
programming is a programming paradigm
that treats computation as the evaluation
of mathematical functions and avoids state
and mutable data. It emphasizes the
application of functions, in contrast to the
imperative programming style, which
emphasizes changes in state.
Sunday, April 15, 12
24. 目前有哪些主流
Functional Language?
Sunday, April 15, 12
25. Erlang
• Ericsson Functional Language
• No mutable variable and side effects
• For distributed, reliable, soft real-time
highly concurrent systems.
• Use the Actor model of concurrency
Sunday, April 15, 12
36. 在FP語言中,變數指
派之後就是 immutable
** Erlang R15B **
X = 1234.
X = 5678.
** exception error: no match of
right hand side value 5678
Sunday, April 15, 12
37. 在 Scala 中,使用 val
宣告變數
val a = 1234
a = 5678
<console>:8: error: reassignment to val
var b = 1234
b = 5678
5678
Sunday, April 15, 12
38. Persistent data
structure
• Immutable 的 Map, Hash, Set 等等
• 跟資料庫無關,而是因為其內部實作特性
• Produce new values by sharing structure
with existing values
Sunday, April 15, 12
39. Why need Immutable
data structure?
• No side-effects 的效能衝擊!!
• Copying overhead
Sunday, April 15, 12
40. Linked List
相加兩個List,會共享已有的資料
Sunday, April 15, 12
41. Tree (內部實作)
建立新的Tree時,共享資料
Sunday, April 15, 12
67. Haskell
add x y = x + y
add 2 3
# 5
addTwo = add 2
addTwo 3
# 5
Sunday, April 15, 12
68. Scala
用⼀一個 _ wildcard
def add(x:Int, y:Int) = x + y
val addTwo = add(2, _:Int)
addTwo(3)
//5
Sunday, April 15, 12
69. Currying
named after Haskell Curry
• 將⼀一個有 N 個參數的 function 轉換成
N 個只有⼀一個參數的 function 的過程
Sunday, April 15, 12
70. Haskell
每個 function 都是 Curried,其實都只有⼀一個參數
max 4 5
# 其實是
(max 4) 5
# max函式的宣告是
max :: Ord a => a -> (a -> a)
max 4 :: (Ord a, Num a) => a -> a
Sunday, April 15, 12
71. Haskell
每個 function 都是 Curried,其實都只有⼀一個參數
max 4 5
(max 4) 先得到⼀一個
# 其實是 Partial Function,然後再
帶入5
(max 4) 5
# max函式的宣告是
max :: Ord a => a -> (a -> a)
max 4 :: (Ord a, Num a) => a -> a
Sunday, April 15, 12
72. Scala
可以將函式定義成 Curried 形式
def sum(a: Int, b: Int) = a + b
sum(2, 3) // 5
def curriedSum(a: Int)(b: Int) = a * b
curriedSum(2)(3) // 5
curriedSum(2)
先得到⼀一個 Partial
Function,然後再帶入3
Sunday, April 15, 12
73. Compose
把兩個⼀一個參數的 Function 合併起來
可以串聯多個組合成新的函數
f(g(x)) = (f。g)(x)
Sunday, April 15, 12
74. Haskell
中間的串列省掉了
reverse( sort [2,5,1,10,5] )
# -- the '.' operator is used to
compose functions
reverseSort = reverse . sort
reverseSort [2,5,1,10,5]
Sunday, April 15, 12
75. JavaScript
也可以自定 compose 方法
function compose(f, g) {
return function(x) {
return f(g(x));
}
}
Sunday, April 15, 12
76. 5. Lazy evaluation
• 直到真的需要才執行
• 增加效率,減少沒有用到的計算
• 可以簡潔地表達無窮 list
Sunday, April 15, 12
77. Haskell is lazy
functional language
cycle [1,2,3]
# [1,2,3, 1,2,3, 1,2,3, 1,2,3, 1,2,3, 1,2,3,
1,2,3,....
take 10 (cycle [1,2,3])
# [1,2,3,1,2,3,1,2,3,1]"
Sunday, April 15, 12
78. Scala’s lazy val
useful for lazy initialization
val a = { println("evaluating A"); "A" }
// evaluating A
// a: java.lang.String = A
lazy val b = { println("evaluating B"); "B" }
// b: java.lang.String = <lazy>
Sunday, April 15, 12
82. Erlang
用來比對然後指派變數
X.
"1: variable 'X' is unbound"
X = 2.
{X, Y} = {1, 2}.
"exception error: no match of right hand side value {1,2}"
{X, Y} = {2, 3}.
{2,3}
Y.
"此時 Y 是 3"
Sunday, April 15, 12
83. Erlang
來試試 List
[H|T] = [1,2,3,4,5].
H. "1"
T. "[2,3,4,5]"
Sunday, April 15, 12
84. Erlang (cont.)
來試試 List
[A,B,C|T] = [1,2,3,4,5].
A. "1"
B. "2"
C. "3"
T. "[4,5]"
Sunday, April 15, 12
85. Scala
用 match 比對
import scala.util.Random
val randomInt = new Random().nextInt(10)
randomInt match {
case 7 => println("lucky seven")
case otherNumber => println("get " + otherNumber)
}
Sunday, April 15, 12
86. Scala
可以比對 Type
val items = List(1, "foo", 3.5)
for (item <- items) {
item match {
case i: Int => println("got an Integer: " + i)
case s: String => println("got a String: " + s)
case f: Double => println("got a Double: " + f)
case other => println("got others: " + other)
}
}
Sunday, April 15, 12
87. Scala
可以加上 Guard 條件
val t1 = ("A", "B")
val t2 = ("C", "D")
val t3 = ("E", "F")
for( tuple <- List(t1, t2, t3) ) {
tuple match {
case (one, two) if one == "C" => println("得到開頭是C的tuple")
case (one, two) => println("blah")
}
}
// blah
// Got tuple starting with C
// blah
Sunday, April 15, 12