# 你想成为一个 函数编程者么（第 1 部分）

#### 纯粹

``````var z = 10;
return x + y;
}
``````

``````function justTen() {
return 10;
}
``````

``````function addNoReturn(x, y) {
var z = x + y
}
``````

``````function add(x, y) {
return x + y;
}
console.log(add(1, 2)); _// still prints 3_
console.log(add(1, 2)); _// WILL ALWAYS print 3_
``````

``````writeFile(fileName);
updateDatabaseTable(sqlCmd);
sendAjaxRequest(ajaxRequest);
``````

#### 不变性

``````var x = 1;
x = x + 1;
``````

``````addOneToSum y z =
let
x = 1
in
x + y + z
``````

let 块中，x 的值一定是 1，也就是说它的值在剩下的生命周期内也是 1。当函数执行完，它的生命周期就结束了，更确切地说，let的块已经求过值了。

in 作用域内，计算包含的值也定义在 let 块级作用域内，也就是，z。返回 x + y + z 的结果，更确切的说，返回1 + y + z ，因为x = 1

“没有变量，现在没有循环？我很你！！！”

``````// simple loop construct
var acc = 0;
for (var i = 1; i <= 10; ++i)
acc += i;
console.log(acc); _// prints 55_
``````
``````// without loop construct or variables (recursion)
function sumRange(start, end, acc) {
if (start > end)
return acc;
return sumRange(start + 1, end, acc + start)
}
console.log(sumRange(1, 10, 0)); _// prints 55_
``````

``````sumRange start end acc =
if start > end then
acc
else
sumRange (start + 1) end (acc + start)
``````

``````sumRange 1 10 0 =      -- sumRange (1 + 1)  10 (0 + 1)
sumRange 2 10 1 =      -- sumRange (2 + 1)  10 (1 + 2)
sumRange 3 10 3 =      -- sumRange (3 + 1)  10 (3 + 3)
sumRange 4 10 6 =      -- sumRange (4 + 1)  10 (6 + 4)
sumRange 5 10 10 =     -- sumRange (5 + 1)  10 (10 + 5)
sumRange 6 10 15 =     -- sumRange (6 + 1)  10 (15 + 6)
sumRange 7 10 21 =     -- sumRange (7 + 1)  10 (21 + 7)
sumRange 8 10 28 =     -- sumRange (8 + 1)  10 (28 + 8)
sumRange 9 10 36 =     -- sumRange (9 + 1)  10 (36 + 9)
sumRange 10 10 45 =    -- sumRange (10 + 1) 10 (45 + 10)
sumRange 11 10 55 =    -- 11 > 10 => 55
55
``````

Up Next: Part 2