forked from DeathKing/Learning-SICP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec2a.srt
3955 lines (3164 loc) · 99.8 KB
/
lec2a.srt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1
00:00:00,000 --> 00:00:05,000
哈尔滨工业大学 IBM技术中心
倾情制作
2
00:00:05,100 --> 00:00:10,000
压制&&特效:蔡钟毓(JohnTitor)
翻译:徐梓翔(Dyul)
3
00:00:10,000 --> 00:00:15,000
特别感谢:裘宗燕教授
时间&&轴校对:邓雄飞(Dysprosium)
4
00:00:15,800 --> 00:00:20,500
高阶过程
Higher-order Procedures
5
00:00:25,280 --> 00:00:26,580
教授:昨天的内容还算容易
PROFESSOR: Well, yesterday was easy.
6
00:00:27,600 --> 00:00:29,440
你们了解到了所有的编程规则
You learned all of the rules of programming
7
00:00:30,740 --> 00:00:33,440
那些几乎是所有的规则了
and lived Almost all of them.
8
00:00:34,600 --> 00:00:37,100
所以此刻 你们算得上是所谓的---
And so at this point, you're now certified programmers
9
00:00:38,060 --> 00:00:38,740
合格的程序员了
-- it says.
10
00:00:39,700 --> 00:00:43,860
不过 我觉得其实是 啊...
However, I suppose what we did is we, aah,
11
00:00:46,720 --> 00:00:50,660
其实只是给你们尝了点甜头
sort of got you a little bit of into an easy state.
12
00:00:51,280 --> 00:00:54,840
此时此刻 你可能还在认为这门课就像
Here, you still believe it's possible that this might be programming
13
00:00:54,840 --> 00:00:57,640
用BASIC语言或者Pascal语言的某种奇葩语法写程序
in BASIC or Pascal with just a funny syntax.
14
00:00:59,140 --> 00:01:04,440
然而就在今天 这种错觉将会被颠覆
Today, that illusion-- or you can no longer support that belief.
15
00:01:04,780 --> 00:01:07,140
我们即将做的事 就是要彻底粉碎这种想法
What we're going to do today is going to completely smash that.
16
00:01:08,080 --> 00:01:13,420
接下来 我会先在黑板上写一些程序
So let's start out by writing a few programs on the blackboard
17
00:01:13,420 --> 00:01:15,020
它们之间有很多相似之处
that have a lot in common with each other
18
00:01:15,960 --> 00:01:18,360
我们要做的是尝试将它们抽象出来
What we're going to do is try to make them abstractions
19
00:01:18,820 --> 00:01:22,960
这过程并不如在其它大多数语言中那么显而易见
that are not ones that are easy to make in most languages.
20
00:01:23,740 --> 00:01:25,080
让我们先从其它语言也能完成的
Let's start with some very simple ones
21
00:01:25,080 --> 00:01:26,520
简单的例子开始
that you can make in most languages.
22
00:01:27,640 --> 00:01:33,680
假设 我有一个求和一组整数的数学表达式
Supposing I want to write the mathematical expression which adds up a bunch of integers.
23
00:01:34,140 --> 00:01:41,100
比如 我写下的这个表达式 以I为索引 求和从A到B的整数
So if I wanted to write down and say the sum from i equal a to b on i.
24
00:01:41,100 --> 00:01:44,820
你们知道用数学公式可以很方便地计算出它的结果
Now, you know that that's an easy thing to compute in a closed form for it,
25
00:01:44,820 --> 00:01:45,760
但我的重点不在此
and I'm not interested in that.
26
00:01:45,760 --> 00:01:48,180
我想要写一个能够求和那些整数程序
But I'm going to write a program that adds up those integers.
27
00:01:48,960 --> 00:01:52,880
我们能够很容易地想到
Well, that's rather easy to do to say
28
00:01:53,300 --> 00:02:07,400
定义求和从A到B的整数的过程SUM-INT为
I want to define the sum of the integers from a to b to be
29
00:02:07,780 --> 00:02:10,640
接下来有两种可能性
well, it's the following two possibilities.
30
00:02:10,900 --> 00:02:13,420
如果A大于B
If a is greater than b,
31
00:02:15,100 --> 00:02:18,220
毋庸置疑 答案就是0
well, then there's nothing to be done and the answer is zero.
32
00:02:18,980 --> 00:02:21,220
你要以递归的方式思考问题
This is how you're going to have to think recursively.
33
00:02:22,160 --> 00:02:25,020
比如 假如我知道某个简单情形的答案
You're going to say if I have an easy case that I know the answer to,
34
00:02:25,140 --> 00:02:25,980
就可以直接将其作为结果
just write it down.
35
00:02:26,220 --> 00:02:30,420
否则 我就需要将这个问题简化
Otherwise, I'm going to try to reduce this problem to a simpler problem.
36
00:02:30,620 --> 00:02:31,560
比如在这个程序里
And maybe in this case,
37
00:02:31,560 --> 00:02:33,240
我要简化出一个子问题
I'm going to make a subproblem of the simpler problem
38
00:02:33,240 --> 00:02:34,620
然后再做一些工作从而得出结果
and then do something to the result.
39
00:02:35,160 --> 00:02:38,700
所以针对这个程序 最简单的处理方式是
So the easiest way to do this is say that
40
00:02:38,700 --> 00:02:43,340
将下标 在这里是A
I'm going to add the index, which in this case is a,
41
00:02:44,520 --> 00:02:57,500
加上A+1到B的整数的求和结果
to the result of adding up the integers from a plus 1 to b.
42
00:03:02,360 --> 00:03:04,860
现在你们应该都能看懂这个定义了
Now, at this point, you should have no trouble looking at such a definition.
43
00:03:05,740 --> 00:03:09,860
实际上 总的来说 要想得出这个过程定义还是有一些困难的
Indeed, coming up with such a thing might be a little hard in synthesis,
44
00:03:10,180 --> 00:03:12,500
但要想读明白还是比较容易的
but being able to read it at this point should be easy.
45
00:03:13,400 --> 00:03:15,880
现在我想告诉你们的是
And what it says to you is, well,
46
00:03:16,440 --> 00:03:18,960
这个是我想要求解的子问题
here is the subproblem I'm going to solve.
47
00:03:19,120 --> 00:03:21,560
我要求和的是
I'm going to try to add up the integers,
48
00:03:21,880 --> 00:03:25,540
比整个问题的规模少一的整数序列
one fewer integer than I added up for the the whole problem.
49
00:03:26,440 --> 00:03:28,100
接下来需要求和的整数序列的数目会一个个减少
I'm adding up the one fewer one,
50
00:03:28,820 --> 00:03:32,680
最后 当这个子问题求解完毕后 只要再加上a
and that subproblem, once I've solved it, I'm going to add a to that,
51
00:03:34,200 --> 00:03:35,920
就能得到整个问题的答案了
and that will be the answer to this problem.
52
00:03:38,140 --> 00:03:40,400
并且 这里的最简单的情形 我不用做任何处理
And the simplest case, I don't have to do any work.
53
00:03:41,560 --> 00:03:45,160
接下来 我要给出另一个类似的简单问题---
Now, I'm also going to write down another simple one just like this,
54
00:03:46,260 --> 00:03:53,400
以I为下标 求和A到B的整数的平方的数学表达式
which is the mathematical expression, the sum of the square from i equal a to b.
55
00:03:55,340 --> 00:03:58,060
同样的 这个程序也很简单
And again, it's a very simple program.
56
00:04:11,180 --> 00:04:13,060
实际上 这个程序一开始和刚才是一样的
And indeed, it starts the same way.
57
00:04:16,220 --> 00:04:19,820
如果A大于B 那么答案就是0
If a is greater than b, then the answer is zero.
58
00:04:20,820 --> 00:04:25,980
显然 你会发现我又把这部分重复写了一遍
And, of course, we're beginning to see that there's something wrong with me writing this down again.
59
00:04:27,280 --> 00:04:28,860
这段程序和之前是相同的
It's the same program.
60
00:04:29,320 --> 00:04:45,900
这里是A的平方加上A+1到B的平方和
It's the sum of the square of a and the sum of the square of the increment and b.
61
00:04:50,440 --> 00:04:54,480
现在 你们再看看这两个程序 它们几乎是完全一样的
Now, if you look at these things, these programs are almost identical.
62
00:04:56,000 --> 00:04:58,860
并没有太大区别
There's not much to distinguish them.
63
00:04:59,760 --> 00:05:04,240
它们有相同的条件表达式、谓词和CONSEQUENCE子句
They have the same first clause of the conditional and the same predicate and the same consequence,
64
00:05:05,580 --> 00:05:07,880
ALTERNATIVE子句也非常地相似
and the alternatives are very similar, too.
65
00:05:08,660 --> 00:05:16,220
事实上 唯一区别是 这里是A 而这里是A的平方
They only differ by the fact that where here I have a, here, I have the square of a.
66
00:05:17,200 --> 00:05:21,620
另一个区别 有些无关紧要
The only other difference, but this one's sort of unessential
67
00:05:21,740 --> 00:05:23,860
是这个过程的名字是SUM-INT
is in the name of this procedure is sum int,
68
00:05:24,540 --> 00:05:26,460
而这个过程的名字是SUM-SQUARE
whereas the name of the procedure is sum square.
69
00:05:27,420 --> 00:05:31,140
所以这两个程序的区别微乎其微
So the things that vary between these two are very small.
70
00:05:32,760 --> 00:05:36,360
现在来看 如果你重复地写了相同的东西
Now, wherever you see yourself writing the same thing down more than once,
71
00:05:36,840 --> 00:05:38,740
这就有些问题 你并不应该那样做
there's something wrong, and you shouldn't be doing it.
72
00:05:39,800 --> 00:05:43,940
问题并不在于你重复的劳动带来时间浪费
And the reason is not because it's a waste of time to write something down more than once.
73
00:05:45,000 --> 00:05:48,900
而是在于里面的一些思想 非常简单的思想
It's because there's some idea here, a very simple idea,
74
00:05:50,260 --> 00:05:55,160
与求和记法相关的思想 就是这一部分
which has to do with the sigma notation-- this much--
75
00:05:56,820 --> 00:05:59,400
其并不依赖于我待求和的内容
not depending upon what it is I'm adding up.
76
00:06:01,280 --> 00:06:05,900
无论何时 当要设计一个复杂的系统并且要弄明白它时
And I would like to be able to-- always, whenever trying to make complicated systems and understand them,
77
00:06:06,220 --> 00:06:09,440
将问题拆分成尽量多的模块是很重要的
it's crucial to divide the things up into as many pieces as I can,
78
00:06:09,640 --> 00:06:11,100
并且每一个模块要能够被独立地解释
each of which I understand separately.
79
00:06:12,640 --> 00:06:16,200
我知道在不依赖具体内容的情况下 把东西加起来的方法
I would like to understand the way of adding things up independently of what it is I'm adding up
80
00:06:17,040 --> 00:06:22,320
这样我就只需要做一次调试 做一次分析
so I can do that having debugged it once and understood it once
81
00:06:23,360 --> 00:06:27,300
还能够与其他用户分享这段程序
and having been able to share that among many different uses of it.
82
00:06:29,040 --> 00:06:30,420
接下来 我有另外一个例子
Here, we have another example.
83
00:06:31,520 --> 00:06:38,800
这是莱布尼茨公式 用来求π/8的值
This is Leibnitz's formula for finding pi over 8.
84
00:06:39,940 --> 00:06:43,500
这一团糟的式子是什么意思?
It's a funny, ugly mess. What is it?
85
00:06:43,500 --> 00:06:54,220
大致是1/(1*3)+1/(5*7)+1/(7*9)+...这样子
It's something like 1 over 1 times 3 plus 1 over 5 times 7 plus 1 over 9 times 11 plus--
86
00:06:54,220 --> 00:07:00,960
有趣的是 根据一些证明 它将收敛到π/8
and for some reason, things like this tend to have interesting values like pi over 8.
87
00:07:01,740 --> 00:07:04,020
我们能发现什么?
But what do we see here?
88
00:07:04,020 --> 00:07:06,980
这个程序或多或少和之前的程序相同
It's the same program or almost the same program.
89
00:07:07,460 --> 00:07:08,920
也是一个求和过程 对吧?
It's a sum. Okay?
90
00:07:08,920 --> 00:07:16,300
这个表达里有一些细微的差别 它的递增的值是4
So we're seeing the figure notation, although over here, we're dealing with incrementing by 4
91
00:07:16,620 --> 00:07:18,040
只是细微的差别而已
so it's a slightly different problem,
92
00:07:18,200 --> 00:07:23,580
所以我们需要在这里将A加4 就在这里
which means that over here, I have to change a by 4, as you see right over here.
93
00:07:25,080 --> 00:07:26,200
不再是加1了
It's not by 1.
94
00:07:27,920 --> 00:07:28,960
当然 另一个区别是
The other thing, of course,
95
00:07:29,260 --> 00:07:33,960
在之前的求平方和的程序里 求和项是平方值
is that the thing that's represented by square in the previous sum of squares,
96
00:07:33,960 --> 00:07:35,640
求整数和的程序里 求和项是整数本身
or a when adding up the integers.
97
00:07:36,020 --> 00:07:38,380
在这里 我用了不同的求和项
Well, here, I have a different thing I'm adding up, a different term,
98
00:07:38,680 --> 00:07:43,060
即1 / (A * (A + 2))
which is 1 over a times a plus 2.
99
00:07:43,940 --> 00:07:45,740
但是 其余部分的程序是相同的
But the rest of this program is identical.
100
00:07:48,140 --> 00:07:50,800
总之 每当我们发现有一些过程是相同的
Well, any time we have a bunch of things like this that are identical,
101
00:07:51,360 --> 00:07:54,240
我们就需要做一些抽象来概括它们
we're going to have to come up with some sort of abstraction to cover them.
102
00:07:55,580 --> 00:08:00,460
回想一下 到目前为止 你们学到的只是一些语法规则
If you think about this, what you've learned so far is the rules of some language,
103
00:08:00,520 --> 00:08:07,600
一些基本表达式 组合的方法 抽象的方法 大概就是这些
some primitive, some means of combination, almost all of them, the means of abstraction, almost all of them.
104
00:08:09,360 --> 00:08:11,700
但是 你们还没学到的是 使用的公共模式
But what you haven't learned is common patterns of usage.
105
00:08:12,880 --> 00:08:15,120
大多时候 你要学习的是一门语言习惯用法
Now, most of the time, you learn idioms when learning a language,
106
00:08:15,120 --> 00:08:19,820
它是一种有价值的公共模式
which is a common pattern that mean things that are useful to know in a flash.
107
00:08:20,780 --> 00:08:23,160
如果你是一个经验丰富的FORTRAN程序员
And if you build a great number of them, if you're a FORTRAN programmer,
108
00:08:23,160 --> 00:08:26,420
你肯定知道
of course, everybody knows how to-- what do you do,
109
00:08:27,240 --> 00:08:30,260
比如 如何求某个数列中的最大值
for example, to get an integer which is the biggest integer in something.
110
00:08:30,860 --> 00:08:32,080
这是经典的问题
It's a classic thing.
111
00:08:32,220 --> 00:08:33,420
每个FORTRAN程序员都知道怎么做
Every FORTRAN programmer knows how to do that.
112
00:08:33,880 --> 00:08:36,820
如果你不知道的话 你可能会陷于困境并花很长的时间想出答案
And if you don't know that, you're in real hot water because it takes a long time to think it out.
113
00:08:37,700 --> 00:08:38,380
然而
However,
114
00:08:39,380 --> 00:08:41,780
在这门语言中我们想展示给你的
one of the things you can do in this language that we're showing you
115
00:08:41,980 --> 00:08:45,600
不是"鱼" 而是"渔"
is not only do you know something like that, but you give the knowledge of that a name.
116
00:08:48,040 --> 00:08:50,040
这就是我们接下来要做的事
And so that's what we're going to be going after right now.
117
00:08:53,020 --> 00:08:55,460
好吧 让我们先看看这些程序的共同点
OK, well, let's see what these things have in common.
118
00:08:58,060 --> 00:09:02,680
在这里我们有一个看似一般的模式
Right over here we have what appears to be a general pattern,
119
00:09:04,040 --> 00:09:07,020
它概括了到目前为止所有的例子
a general pattern which covers all of the cases we've seen so far.
120
00:09:09,380 --> 00:09:13,100
这里定义了一个求和过程
There is a sum procedure, which is being defined.
121
00:09:14,380 --> 00:09:18,040
它有两个参数 代表求和的下界和上界
It has two arguments, which are a lower bound and an upper bound.
122
00:09:19,380 --> 00:09:22,980
首先判断下界是否大于上界
The lower bound is tested to be greater than the upper bound,
123
00:09:22,980 --> 00:09:26,680
如果下界大于上界的话 结果就是0
and if it is greater, then the result is zero.
124
00:09:27,200 --> 00:09:31,080
否则 我们要对下界做一些处理
Otherwise, we're going to do something to the lower bound,
125
00:09:31,080 --> 00:09:33,540
也就是下标
which is the index of the conversation,
126
00:09:34,200 --> 00:09:40,540
将处理后的结果与后面这个过程的结果递归地相加
and add that result to the result of following the procedure recursively
127
00:09:41,300 --> 00:09:45,340
这个过程的参数是NEXT操作处理后的下界
on our lower bound incremented by some next operation
128
00:09:47,560 --> 00:09:49,400
以及与之前相同的上界
with the same upper bound as I had before.
129
00:09:53,380 --> 00:09:56,760
这即是公共模式
So this is a general pattern,
130
00:09:57,540 --> 00:10:00,980
我想给这个公共模式命名
and what I'd like to do is be able to name this general pattern a bit.
131
00:10:03,180 --> 00:10:04,120
这还挺简单
Well, that's sort of easy,
132
00:10:05,000 --> 00:10:08,020
因为我要做的是...
because one of the things I'm going to do right now is--
133
00:10:08,020 --> 00:10:10,020
数字不是非常特殊的东西
there's nothing very special about numbers.
134
00:10:11,340 --> 00:10:13,100
数字仅仅是一种数据
Numbers are just one kind of data.
135
00:10:14,280 --> 00:10:20,300
为各种数据命名看上去也是很合理的事情
It seems to be perfectly reasonable to give all sorts of names to all kinds of data,
136
00:10:21,000 --> 00:10:22,140
比如说 “过程”
for example, procedures.
137
00:10:22,700 --> 00:10:25,680
并且当今很多语言都允许使用过程参数
And now many languages allow you have procedural arguments,
138
00:10:25,900 --> 00:10:28,460
现在 我们即将讨论过程参数
and right now, we're going to talk about procedural arguments.
139
00:10:29,020 --> 00:10:30,100
它们很容易处理
They're very easy to deal with.
140
00:10:30,860 --> 00:10:33,880
我们首先不去考虑过程参数 来做一些很特别的事情
And shortly, we'll do some remarkable things that are not like procedural arguments.
141
00:10:35,420 --> 00:10:41,700
这里 定义我们的求和记法
So here, we'll define our sigma notation.
142
00:10:42,780 --> 00:10:59,740
过程名叫做SUM 参数为TERM A NEXT和B
This is called sum and it takes a term, an A, a next term, and B as arguments.
143
00:10:59,740 --> 00:11:00,900
所以 它有四个参数
So it takes four arguments,
144
00:11:02,460 --> 00:11:05,520
这里我写成小写没有特殊的含义
and there was nothing particularly special about me writing this in lowercase.
145
00:11:06,020 --> 00:11:09,440
我不希望它迷惑你 所以我现在把它改成大写
I hope that it doesn't confuse you, so I'll write it in uppercase right now.
146
00:11:09,780 --> 00:11:10,680
机器并不关心大小写
The machine doesn't care.
147
00:11:14,140 --> 00:11:17,860
但这两个参数并不同 它们不是数字
But these two arguments are different. These are not numbers.
148
00:11:18,840 --> 00:11:22,140
它们是对数字进行计算的过程
These are going to be procedures for computing something given a number.
149
00:11:23,300 --> 00:11:25,700
TERM是这样一个过程 提供它一个下标
Term will be a procedure which, when given an index,
150
00:11:26,140 --> 00:11:28,660
TERM会计算出这个下标所对应的项的值
produce the value of the term for that index.
151
00:11:29,440 --> 00:11:32,420
NEXT过程会根据一个下标计算下一个下标
Next will be given an index, which will produce the next index.
152
00:11:33,560 --> 00:11:34,740
这是用来计数的
This will be for counting.
153
00:11:35,540 --> 00:11:36,720
这很简单
And it's very simple.
154
00:11:40,060 --> 00:11:41,740
就是字面的意思
It's exactly what you see.
155
00:11:42,900 --> 00:11:50,800
如果A大于B 结果就是0
If A is greater than B, then the result is 0.
156
00:11:51,820 --> 00:12:10,000
否则 结果是(TERM A)与(SUM TERM (NEXT A)...)的和
Otherwise, it's the sum of term applied to A and the sum of term, next index.
157
00:12:15,120 --> 00:12:16,140
我换个方式写
Let me write it this way.
158
00:12:29,580 --> 00:12:31,660
现在 首先我要你们看一些东西
Now, I'd like you to see something, first of all.
159
00:12:31,820 --> 00:12:33,640
我写到黑板这 然后写不下了
I was writing here, and I ran out of space.
160
00:12:34,740 --> 00:12:38,640
于是我使用了缩进 依据的是整齐打印规则
What I did is I start indenting according to the Pretty-printing rule,
161
00:12:38,640 --> 00:12:41,980
意思是我把过程的每个参数对齐
which says that I align all of the arguments of the procedure
162
00:12:43,360 --> 00:12:45,280
这样我就能看清它们的层次
so I can see which ones go together.
163
00:12:46,600 --> 00:12:48,400
这是我刚才无意识地做下的
And this is just something I do automatically,
164
00:12:49,080 --> 00:12:50,220
并且我也希望你们学会这样做
and I want you to learn how to do that, too,
165
00:12:50,220 --> 00:12:51,940
这样你的程序就便于阅读和理解
so your programs can be read and understood.
166
00:12:53,800 --> 00:12:56,900
现在 看看我们有什么
However, what do we have here?
167
00:12:57,200 --> 00:13:02,100
我们有四个参数:过程、下界的下标、
We have four arguments: the procedure, the lower index- - lower bound index--
168
00:13:03,260 --> 00:13:06,180
获得下一个下标的方法 以及上界
the way to get the next index, and the upper bound.
169
00:13:08,440 --> 00:13:14,300
在递归调用的部分实际上传递的是同一个过程
What's passed along on the recursive call is indeed the same procedure
170
00:13:14,960 --> 00:13:16,120
因为我再一次需要它了
because I'm going to need it again,
171
00:13:17,120 --> 00:13:20,140
下一个下标 是用NEXT过程计算出的
the next index, which is using the next procedure to compute it,
172
00:13:20,840 --> 00:13:24,100
计算下一个下标的过程 我也单独地需要 因为这两者是不同的
the procedure for computing next, which I also have to have separately, and that's different.
173
00:13:24,820 --> 00:13:28,200
计算下一个下标的过程与下一个下标是不同的
The procedure for computing next is different from the next index,
174
00:13:28,680 --> 00:13:30,920
下一个下标是NEXT过程应用于上一个下标所产生的结果
which is the result of using next on the last index.
175
00:13:31,960 --> 00:13:33,800
最后我也需要传递上界
And I also have to pass along the upper bound.
176
00:13:36,600 --> 00:13:45,300
于是 这就囊括了所有的这些 以及我们写过的其它的漂亮的程序
So this captures both of these and the other nice program that we are playing with.
177
00:13:47,440 --> 00:13:55,860
利用这个 我们就可以很容易地写出SUM的原始实例程序
So using this, we can write down the original program as instances of sum very simply.
178
00:14:08,700 --> 00:14:10,000
A和B
A and B.
179
00:14:15,260 --> 00:14:20,180
好吧 我在这需要一个IDENTITY过程 因为...
Well, I'm going to need an identity procedure here because ,ahh,
180
00:14:25,520 --> 00:14:31,740
整数的求和需要我在这里对每一个整数计算一次TERM
the sum of the integers requires me to in this case compute a term for every integer,
181
00:14:32,060 --> 00:14:34,380
但是这个TERM过程并不对这个整数做任何改变
but the term procedure doesn't want to do anything to that integer.
182
00:14:35,240 --> 00:14:37,920
所以关于A的IDENTITY过程结果就是A
So the identity procedure on A is A
183
00:14:39,000 --> 00:14:40,280
写成X或者其它的符号也没关系
or X or whatever,
184
00:14:40,840 --> 00:14:46,400
接下来 将IDENTITY作为SUM的TERM过程
and I want to say the sum of using identity of the term procedure
185
00:14:52,040 --> 00:14:53,860
用A作为初始下标
and using A as the initial index
186
00:14:55,300 --> 00:15:00,780
1+过程用来获取下一个下标
and the incrementer being the way to get the next index
187
00:15:01,620 --> 00:15:06,360
B作为上界
and B being the high bound, the upper bound.
188
00:15:07,460 --> 00:15:12,340
这个过程与这里的SUM-INT过程工作方式相同
This procedure does exactly the same as the sum of the integers over here,
189
00:15:12,580 --> 00:15:13,720
计算出的答案也相同
computes the same answer.
190
00:15:17,200 --> 00:15:20,220
现在 值得注意的一点是
Now, one thing you should see, of course,
191
00:15:20,620 --> 00:15:25,380
这里的形式参数写成什么都无所谓
is that there's nothing very special over here about what I used as the formal parameter.
192
00:15:25,380 --> 00:15:28,800
比如 我可以把它写成X 也没关系
I could have, for example, written this X. It doesn't matter.
193
00:15:29,060 --> 00:15:34,240
注意到这个X和那个X并不冲突
I just wanted you to see that this name does not conflict with this one at all.
194
00:15:34,620 --> 00:15:35,820
这是一个内部名称
It's an internal name.
195
00:15:37,480 --> 00:15:41,060
对于第二个过程---平方和 它甚至更简单一些
For the second procedure here, the sum of the squares, it's even a little bit easier.
196
00:15:53,280 --> 00:15:58,020
我们需要做的仅仅是将平方项加起来
And what do we have to do? Nothing more than add up the squares,
197
00:16:00,740 --> 00:16:05,500
这个过程会对每个下标生效
this is the procedure that each index will be given, will be given each-- yes.
198
00:16:06,180 --> 00:16:09,220
每个下标使用这个过程来产生一个项
Each index will have this done to it to get the term.
199
00:16:09,940 --> 00:16:12,420
它对应了那里的TERM
That's the thing that maps against term over here.
200
00:16:13,140 --> 00:16:14,840
然后将A作为下界
Then I have A as the lower bound,