# 如何设计一门语言（七）——闭包、lambda和interface - λ-calculus（惊愕到手了欧耶，GetBlogPostIds.aspx） - C++博客

```void Swap(int a, int b)
{
int t = a;
a = b;
b = t;
}

int main()
{
int a=0;
int b=1;
Swap(a, b);
printf("%d, %d", a, b);
}```

```int Add(int a, int b)
{
return a + b;
}

int Choose(bool first, int a, int b)
{
return first ? a : b;
}

int main()
{
printf("%d", r);
}```

```int Set(int a, int b, int c, int d)
{
a += b;
a += c;
a += d;
}

int main()
{
int i = 0;
int x[3] = {1, 2, 3};
Set(x[i++], 10, 100, 1000);
printf("%d, %d, %d, %d", x[0], x[1], x[2], i);
}```

```x[i++] += 10;
x[i++] += 100;
x[i++] += 1000;```

```Action<int> SetX()
{
int x = 0;
return (int n)=>
{
x = n;
};
}

void Main()
{
int x = 10;
var setX = SetX();
setX(20);
Console.WriteLine(x);
}```

=======================我是背景知识的分割线=======================

```function pair(a, b) {
return function(c) {
return c(a, b);
};
}

function first(a, b) {
return a;
}

function second(a, b) {
return b;
}

var p = pair(1, pair(2, 3));
var a = p(first);
var b = p(second)(first);
var c = p(second)(second);
print(a, b, c);```

```var p = pair(1, pair(2, 3));
var a = p(first);

// ↓↓↓↓↓

var p = function(c) {
return c(1, pair(2, 3));
};
var a = p(first);

// ↓↓↓↓↓

var a = first(1, pair(2, 3));

// ↓↓↓↓↓

var a = 1;```

```var p = {
first : 1,
second : {
first : 1,
second : 2,
}
};

var a = p.first;
var b = p.second.first;
var c = p.second.second;```

```// 在这里我们继续使用上面的pair、first和second函数

function mutable_pair(a, b) {
return function(c) {
var x = c(a, b);
// 这里我们把pair当链表用，一个(1, 2, 3)的链表会被储存为pair(1, pair(2, pair(3, null)))
a = x(second)(first);
b = x(second)(second)(first);
return x(first);
};
}

function get_first(a, b) {
return pair(a, pair(a, pair(b, null)));
}

function get_second(a, b) {
return pair(b, pair(a, pair(b, null)));
}

function set_first(value) {
return function(a, b) {
return pair(undefined, pair(value, pair(b, null)));
};
}

function set_second(value) {
return function(a, b) {
return pair(undefined, pair(a, pair(value, null)));
};
}

var p = mutable_pair(1, 2);
var a = p(get_first);
var b = p(get_second);
print(a, b);
p(set_first(3));
p(set_second(4));
var c = p(get_first);
var d = p(get_second);
print(c, d);```

```var p = mutable_pair(1, 2);
p(set_first(3));

// ↓↓↓↓↓

p = return function(c) {
var x = c(1, 2);
a = x(second)(first);
b = x(second)(second)(first);
return x(first);
};
p(set_first(3));

// ↓↓↓↓↓

var x = set_first(3)(1, 2);
p.a = x(second)(first); // 这里的a和b是p的闭包内包含的上下文的变量了，所以这么写会清楚一点
p.b = x(second)(second)(first);
// return x(first);出来的值没人要，所以省略掉。```
```
// ↓↓↓↓↓

var x = (function(a, b) {
return pair(undefined, pair(3, pair(b, null)));
})(1, 2);
p.a = x(second)(first);
p.b = x(second)(second)(first);

// ↓↓↓↓↓

x = pair(undefined, pair(3, pair(2, null)));
p.a = x(second)(first);
p.b = x(second)(second)(first);

// ↓↓↓↓↓

p.a = 3;
p.b = 2;```

```function rectangle(a, b) {
this.width = a;
this.height = height;
}

rectangle.prototype.get_area = function() {
return this.width * this.height;
};

var r = new rectangle(3, 4);
print(r.get_area());```

```var x = r.get_area;
print(x());```

```class Rectangle
{
public int width;
public int height;

public int GetArea()
{
return width * height;
}
};```

```var r = new Rectangle
{
width = 3;
height = 4;
};

// 第一段代码
Console.WriteLine(r.GetArea());

// 第二段代码
Func<int> x = r.GetArea;
Console.WriteLine(x());```

```Func<int> x = ()=>
{
return r.GetArea();
};
Console.WriteLine(x());```

```function mutable_pair(a, b) {
_this = {
get_first = function() { return a; },
get_second = function() { return b; },
set_first = function(value) { a = value; },
set_second = function(value) { b = value; }
};    return _this;
}

var p = new mutable_pair(1, 2);
var a = p.get_first();
var b = p.get_second();
print(a, b);
var c = p.set_first(3);
var d = p.set_second(4);
print(c, d);```

```var x = p.set_first;
var y = p.set_second;
x(3);

::...

ScrapBook :: Firefox Extension

或是邮件反馈可也:

粤ICP备18025058号-1

44049002000656

...::
```