forked from DeathKing/Learning-SICP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec6b.srt
5787 lines (4627 loc) · 123 KB
/
lec6b.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,032 --> 00:00:03,728
Learning-SICP学习小组
倾情制作
2
00:00:03,984 --> 00:00:07,264
翻译&&时间轴:张大伟(DreamAndDead)
压制&&特效:邓雄飞(Dysprosium)
校对:邓雄飞(Dysprosium)
3
00:00:07,408 --> 00:00:10,272
特别感谢:裘宗燕教授
4
00:00:10,400 --> 00:00:14,928
计算机程序的构造和解释
5
00:00:15,056 --> 00:00:19,408
流 II
Stream
6
00:00:20,970 --> 00:00:24,080
教授:上节课 我们介绍了流
PROFESSOR: OK, well, we've been looking at streams,
7
00:00:24,080 --> 00:00:27,824
按照信号处理的方式来组织系统
this signal processing way of putting systems together.
8
00:00:28,870 --> 00:00:31,424
要记住的是 关键点在于
And remember, the key idea is that
9
00:00:31,904 --> 00:00:32,960
我们分离开
we decouple
10
00:00:34,208 --> 00:00:37,312
程序中 事件表面上的顺序
the apparent order of events in our programs
11
00:00:37,584 --> 00:00:40,176
与机器中的实际计算顺序
from the actual order of events in the computer.
12
00:00:41,072 --> 00:00:42,288
那就意味着 我们可以
And that means that we can start
13
00:00:42,576 --> 00:00:44,144
着手处理非常长的流
dealing with very long streams
14
00:00:44,896 --> 00:00:47,392
并且只有在需要的时候才生成其中的元素
and only having to generate the elements on demand.
15
00:00:47,536 --> 00:00:49,392
这种按需计算的方式
That sort of on-demand computation
16
00:00:49,520 --> 00:00:51,408
是内建在流的数据结构中的
is built into the stream's data structure.
17
00:00:54,110 --> 00:00:55,648
即使这个流非常之长
So if we have a very long stream,
18
00:00:55,664 --> 00:00:57,080
我们只计算所需要的
we only compute what we need.
19
00:00:58,040 --> 00:01:00,750
只有当我们要求的时候 新的数据才会生成
The things only get computed when we actually ask for them.
20
00:01:00,750 --> 00:01:01,744
要举个什么样的例子呢?
Well, what are examples?
21
00:01:02,110 --> 00:01:03,600
这个“按需”是什么个情况呢?
Are they actually asking for them?
22
00:01:05,024 --> 00:01:06,016
举个例子
For instance, we might
23
00:01:09,216 --> 00:01:11,376
我们可能会想要一个流中的第N个元素
might ask for the n-th element of a stream.
24
00:01:15,360 --> 00:01:18,928
这个过程可以用于计算流的第N个元素
Here's a procedure that computes the n-th element of a stream.
25
00:01:20,090 --> 00:01:21,232
一个参数为索引N
An integer n,
26
00:01:21,248 --> 00:01:22,848
另一个参数是流S
the n-th element of some stream s,
27
00:01:23,408 --> 00:01:25,424
递归遍历这个流即可求解
and we just recursively walk down the stream.
28
00:01:25,570 --> 00:01:27,392
如果N为0 我们就计算头部分
And if n is 0, we compute the head.
29
00:01:27,960 --> 00:01:30,992
否则 就在流的尾部分
Otherwise, it's the n-th the minus 1 element
30
00:01:31,744 --> 00:01:32,800
查找第N-1个元素
of the stream.
31
00:01:34,310 --> 00:01:36,432
看起来是Lisp中很普通的编程方式 但是不同的是
Those two are just like for Lisp, but the difference
32
00:01:36,624 --> 00:01:38,768
直到我们不断遍历 取得相继的N个元素
is those elements aren't going to get computed
33
00:01:38,864 --> 00:01:40,992
这些元素才被计算出来
until we walk down, taking successive n-ths.
34
00:01:41,520 --> 00:01:44,784
这是这些流元素可能被FORCE的一种方式
So that's one way that the stream elements might get forced.
35
00:01:45,776 --> 00:01:46,640
另外一种方式则是
And another way,
36
00:01:47,184 --> 00:01:48,928
这里有个简单的过程 用来打印一个流
here's a little procedure that prints a stream.
37
00:01:49,300 --> 00:01:50,384
它的定义是
We say print a stream,
38
00:01:51,904 --> 00:01:53,280
过程PRINT-STREAM的定义是
so to print a stream s.
39
00:01:54,150 --> 00:01:55,120
我们要怎么做呢?
Well, what do we do? We'll
40
00:01:55,744 --> 00:01:56,864
先打印流的头部分
We print the head of the stream,
41
00:01:57,744 --> 00:01:59,328
流的头部分在这时就被计算出来
and that will cause the head to be computed.
42
00:01:59,720 --> 00:02:02,848
然后我们再递归地打印流的尾部分
And then we recursively print stream the tail of the stream.
43
00:02:04,990 --> 00:02:06,032
完成以后
And if we're already done,
44
00:02:06,048 --> 00:02:08,576
就返回一个的表示完成的消息 “DONE”
maybe we have to return something about the message done.
45
00:02:09,660 --> 00:02:11,392
如果你构造了一个流
OK, and then so if you make a stream,
46
00:02:11,648 --> 00:02:13,648
这个流非常的长
you could say here's the stream, this very long stream.
47
00:02:14,310 --> 00:02:16,336
当你调用这个过程
And then you say print the stream,
48
00:02:16,416 --> 00:02:19,776
流中的元素会随着PRINT-STREAM的调用
and the elements of the stream will get computed successively
49
00:02:19,872 --> 00:02:21,120
而被依次计算出来
as that print calls them.
50
00:02:21,320 --> 00:02:22,816
不会在一开始就全部计算出来
They won't get all computed initially.
51
00:02:24,300 --> 00:02:25,664
正因为如此 我们能够
So in this way, we can
52
00:02:27,504 --> 00:02:29,610
我们能够处理非常长的流
So in this way, we can deal with some very long streams.
53
00:02:30,190 --> 00:02:31,920
多长呢?
Well, how long can a stream be?
54
00:02:33,744 --> 00:02:35,120
可以是无限长
Well, it can be infinitely long.
55
00:02:35,904 --> 00:02:38,048
我们在计算机上实践一下
Let's look at an example here on the computer.
56
00:02:38,920 --> 00:02:41,968
我可以在计算机前输入
I could walk up to this computer, and I could say--
57
00:02:43,488 --> 00:02:53,312
我先定义一个函数 (INTEGERS-FROM N)
how about we'll define the stream of integers starting with some number N,
58
00:02:54,240 --> 00:02:57,136
用于生成一个从N开始的正整数流
the stream of positive integers starting with some number n.
59
00:03:00,360 --> 00:03:19,168
也就是 (CONS-STREAM N (INTEGERS-FROM (+ N 1))))
And that's cons-stream of n onto the integers from one more.
60
00:03:24,416 --> 00:03:25,616
这样就我们要的全部整数
So there are the integers.
61
00:03:28,992 --> 00:03:31,500
现在我来尝试得到所有的整数
Then I could say let's get all the integers.
62
00:03:34,570 --> 00:03:44,336
(DEFINE INTEGERS (INTEGERS-FROM 1))
define the stream of integers to be the integers starting with 1.
63
00:03:48,840 --> 00:03:50,944
如果现在我执行 (NTH-STREAM 20 INTEGERS)
And now if I say something like
64
00:03:54,416 --> 00:03:55,800
来查看第20个元素
what's the what's the 20th integer.
65
00:04:03,424 --> 00:04:05,536
得到21 因为索引是从0开始的
So it's 21 because we start counting at 0.
66
00:04:06,848 --> 00:04:08,880
或者我们来点更复杂的
Or I can do more complicated things.
67
00:04:09,450 --> 00:04:10,840
我再来定义一个谓词
Let me to define a little predicate here.
68
00:04:11,776 --> 00:04:18,512
谓词NO-SEVEN用来检测是否为7的倍数
How about define no-seven.
69
00:04:19,580 --> 00:04:20,752
它的判定方法是这样的:
It's going to test an integer,
70
00:04:21,792 --> 00:04:23,168
如果整数X不是7的倍数
and it's going to say it's not.
71
00:04:28,820 --> 00:04:33,968
我取X除7的余数
I take the remainder of x by 7,
72
00:04:36,624 --> 00:04:38,352
余数不应该为0
I don't get 0.
73
00:04:43,808 --> 00:04:49,776
这时用NO-SEVEN这个谓词
And then I could say define the integers with no sevens
74
00:04:50,224 --> 00:04:59,120
过滤全部的整数
take all the integers and filter them to have no sevens.
75
00:05:11,570 --> 00:05:13,344
这样我就得到了所有的
So now I've got the stream of all the integers
76
00:05:13,632 --> 00:05:15,056
不是7的倍数的整数构成的流
that are not divisible by seven.
77
00:05:16,490 --> 00:05:23,440
如果我问 这些不是7的倍数的整数中
So if I say what's the 100th integer
78
00:05:24,704 --> 00:05:26,480
的第100个数是多少?
and the list not divisible by seven,
79
00:05:26,864 --> 00:05:28,112
结果是117
I get 117.
80
00:05:28,320 --> 00:05:30,672
或者我也可以问
Or if I'd like to say well, I could say ah.
81
00:05:32,304 --> 00:05:34,384
这个流的所有元素都是些什么?
well, gee, what are all of them?
82
00:05:35,270 --> 00:05:40,352
我可以用(PRINT-STREAM NS)来尝试打印这个流
So I could say print stream all these integers with no seven,
83
00:05:40,832 --> 00:05:41,792
它就会输出个不停
it goes off printing.
84
00:05:45,100 --> 00:05:47,070
你可能需要等上很久才能得到全部结果
You may have to wait a very long time to see them all.
85
00:05:52,670 --> 00:05:53,840
你可能会问了
Well, you can start asking, gee,
86
00:05:54,816 --> 00:05:57,008
这个数据结构
you know, is it really true that this data structure
87
00:05:58,288 --> 00:06:00,656
真的全部是由整数构成的吗?
with the integers is really all the integers?
88
00:06:01,100 --> 00:06:04,053
现在我画一个图来演示下刚写的那个程序
And let me draw a picture of that program I just wrote.
89
00:06:04,960 --> 00:06:10,570
这是我刚才键入的整数定义
Here's the, right, here's the definition of the integers again that I just typed in,
90
00:06:12,336 --> 00:06:15,984
它是一个由第一个整数和由下一个整数生成的流 所构成的序对
Right it's a cons of the first integer under the integer starting with the rest.
91
00:06:17,616 --> 00:06:19,770
现在我们画个图来看看它到底是什么样
Now, we can make a picture of that and see what it looks like.
92
00:06:22,720 --> 00:06:24,320
从概念上来说 这应该是一个盒子
Conceptually, what I have is a box
93
00:06:25,536 --> 00:06:27,184
这个盒子是(INTEGER-FROM N)
that's the integer starting with n.
94
00:06:27,420 --> 00:06:29,088
它接受一个参数N
It takes in some number n,
95
00:06:31,424 --> 00:06:32,976
然后返回一个流
and it's going to return a stream of--
96
00:06:35,024 --> 00:06:37,360
这个无穷流表示从N开始的所有整数
this infinite stream of all integers starting with n.
97
00:06:38,080 --> 00:06:38,736
我要做什么呢?
And what do I do?
98
00:06:38,752 --> 00:06:42,384
呃 这个是INT-FROM盒子
Well, this is an integers-from box.
99
00:06:45,070 --> 00:06:45,800
里面是什么样子呢?
What's it got in it?
100
00:06:45,800 --> 00:06:48,608
取得参数N之后
Well, it takes in this n,
101
00:06:52,272 --> 00:06:53,920
将其 +1
and it increments it.
102
00:06:57,952 --> 00:07:03,150
然后把结果递归地传递给另一个INT-FROM盒子
And then it puts the result into recursively another integer's from box.
103
00:07:06,870 --> 00:07:09,600
把这个盒子的结果和最初的N
It takes the result of that and the original n
104
00:07:10,240 --> 00:07:12,784
用CONS组合起来
and puts those together with a cons
105
00:07:13,392 --> 00:07:14,360
就形成了一个流
and forms a stream.
106
00:07:14,576 --> 00:07:17,264
我刚才写的那个过程 画出来就是这样子
So that's a picture of that program I wrote. And this is a ...
107
00:07:18,528 --> 00:07:20,320
我们看到的这类图像
Let's see. These kind of diagrams we first saw
108
00:07:20,784 --> 00:07:21,740
首先是由Peter Henderson提出的
drawn by Peter Henderson,
109
00:07:21,760 --> 00:07:23,320
也就是前面课程中绘图语言的发明者
the same guy who did the Escher language.
110
00:07:23,320 --> 00:07:24,752
我们把这种图叫做Henderson图
We call them Henderson diagrams.
111
00:07:25,376 --> 00:07:27,904
画这种图需要遵守一定的约定
And the convention here is that you put these things together.
112
00:07:28,530 --> 00:07:32,512
这些实线代表输出的流
And the solid lines are things coming out are streams,
113
00:07:33,024 --> 00:07:36,208
这些虚线则是初始的输入值
and dotted lines are initial values going in.
114
00:07:37,270 --> 00:07:39,024
而这个图描述的形状是——
So this one has the shape of--
115
00:07:39,408 --> 00:07:41,600
它会取一个整数作为初始值
it takes in some integer, some initial value,
116
00:07:41,808 --> 00:07:42,912
然后输出一个流
and outputs a stream.
117
00:07:46,352 --> 00:07:48,224
现在 你可能又要问了
Again, you can ask. You know it's really
118
00:07:48,380 --> 00:07:50,880
那个INTEGERS的数据结构真的全部都是整数吗?
Is that data structure integers really all the integers?
119
00:07:52,090 --> 00:07:54,912
或者它只是经过了精心组织
Alright? Or is it is something that's cleverly arranged
120
00:07:54,940 --> 00:07:56,432
以至于总可以在其中找到
so that whenever you look for an integer
121
00:07:56,448 --> 00:07:57,240
我们需要的那个整数?
you find it there?
122
00:07:57,950 --> 00:07:59,744
这有点像个哲学问题 不是么?
That's sort of a philosophical question, right?
123
00:07:59,780 --> 00:08:01,696
如果有一个东西
If something is there
124
00:08:02,144 --> 00:08:03,968
你不去观测它 能否知道它“存在”呢?
whenever you look, is it really there or not?
125
00:08:04,450 --> 00:08:07,344
这就有点像
It's sort of the same sense in which
126
00:08:07,360 --> 00:08:09,420
你在银行中的存款那样
the money in your savings account is in the bank.
127
00:08:12,380 --> 00:08:12,640
好吧
Well
128
00:08:16,352 --> 00:08:17,488
我们再来看一个例子
let me do another example.
129
00:08:18,680 --> 00:08:20,704
这门课的第一节课
Umm, Gee, we started the course
130
00:08:20,720 --> 00:08:22,720
我们就讲了一个来自于亚历山大的算法
with an algorithm from Alexandria,
131
00:08:23,296 --> 00:08:25,800
来自亚历山大的Heron提出的
which was Heron of Alexandria's algorithm
132
00:08:25,824 --> 00:08:26,944
一个用于计算平方根的算法
for computing the square root.
133
00:08:28,470 --> 00:08:32,030
现在再来看一个 同样来自于亚力山大的算法
Let's take a look at another Alexandrian algorithm.
134
00:08:32,030 --> 00:08:35,088
这个被称为Eratosthenes算法的方法
This one is Eratosthenes method for
135
00:08:36,192 --> 00:08:38,448
用于计算所有的质数
for computing all of the primes.
136
00:08:41,169 --> 00:08:42,830
它被称为Eratosthenes筛法
It is called the Sieve of Eratosthenes.
137
00:08:42,830 --> 00:08:49,728
它是这样的 一开始
And what you do is you start out,
138
00:08:50,992 --> 00:08:52,288
先列举所有的整数
and you list all the integers,
139
00:08:52,608 --> 00:08:53,536
从2开始
say, starting with 2.
140
00:08:53,880 --> 00:08:55,040
然后取第一个整数
And then you take the first integer, and you say,
141
00:08:55,088 --> 00:08:56,670
然后你发现 哦 2是一个质数
and you say, oh, that's prime.
142
00:08:57,310 --> 00:08:58,352
然后你考察剩余的整数
And then you go look at the rest,
143
00:08:58,688 --> 00:09:00,880
划掉其中可以被2整除的数
and you cross out all the things divisible by 2.
144
00:09:01,520 --> 00:09:04,736
我把这个划掉 还有这个 这个
So I cross out this and this and this.
145
00:09:05,250 --> 00:09:06,352
有点费时
This takes a long time
146
00:09:06,368 --> 00:09:08,912
我要对所有的整数进行这样的操作
because I have to do it for all of the integers.
147
00:09:11,160 --> 00:09:15,392
我遍历整个整数表
So I go through the entire list of integers,
148
00:09:18,272 --> 00:09:20,944
划掉所有被2整除的数
crossing the ones divisible by 2.
149
00:09:22,112 --> 00:09:24,384
所有的整数都操作完后
And now when I finish with all of the integers,
150
00:09:24,784 --> 00:09:26,720
回过头再来看还剩些什么
I go back and look and say what am I left with?
151
00:09:27,040 --> 00:09:28,800
好的 下一个数就是3了
Well, the first thing that starts there is 3.
152
00:09:29,330 --> 00:09:30,336
3也是一个质数
So 3 is a prime.
153
00:09:30,770 --> 00:09:33,056
现在 我会继续在剩下的数中
And now I go back through what I'm left with,
154
00:09:33,360 --> 00:09:35,072
划掉所有被3整除的数
and I cross out all the things divisible by 3.
155
00:09:35,080 --> 00:09:43,808
划掉 9、15、21、27、33 等等
So let's see, 9 and 15 and 21 and 27 and 33 and so on.
156
00:09:44,336 --> 00:09:45,120
我就不往下划了
I won't finish.
157
00:09:45,350 --> 00:09:46,528
然后看看我们还剩下什么
Then I see what I'm left with.
158
00:09:47,250 --> 00:09:49,840
而下一个就是5了
And the next one I have is 5.
159
00:09:50,496 --> 00:09:52,048
我又遍历剩下的数
Now I can through the rest,
160
00:09:52,432 --> 00:09:54,512
找到第一个能被5整除的数
and I find the first one that's divisible by 5.
161
00:09:54,540 --> 00:09:57,616
把剩下的能被5整除的数都划掉
I cross out from the remainder all the ones that are divisible by 5.
162
00:09:58,352 --> 00:09:59,248
做完这个之后
And I did that,
163
00:09:59,824 --> 00:10:01,890
下一个数就是7
and then I go through and find 7.
164
00:10:01,890 --> 00:10:02,720
再遍历剩下的数
Go through all the rest,
165
00:10:02,760 --> 00:10:03,952
划掉所有被7整除的数
cross out things divisible 7,
166
00:10:03,984 --> 00:10:05,470
然后一直这样下去
and I keep doing that forever.
167
00:10:06,810 --> 00:10:07,408
全部结束的时候
And when I'm done,
168
00:10:07,408 --> 00:10:09,104
我也就得到了所有的质数
what I'm left with is a list of all the primes.
169
00:10:09,904 --> 00:10:13,312
这就是Eratosthenes筛法
So that's the Sieve of Eratosthenes.
170
00:10:15,430 --> 00:10:17,696
我们来看下实际代码
Let's look at it as a computer program.
171
00:10:17,930 --> 00:10:19,856
这个过程命名为SIEVE
It's a procedure called sieve.
172
00:10:27,910 --> 00:10:29,408
这是对应的代码
Now, I just write what I did.
173
00:10:30,336 --> 00:10:34,480
SIEVE过程 以一个流S为参数
I'll say to sieve some stream s.
174
00:10:38,770 --> 00:10:39,936
返回一个新的流
I'm going to build a stream
175
00:10:40,272 --> 00:10:41,840
新的流的头部分 就是流S的头部分
whose first element is the head of this.
176
00:10:41,870 --> 00:10:44,432
回忆一下 我总是取剩下的数中的第一个
Remember, I always found the first thing I was left with,
177
00:10:44,912 --> 00:10:48,752
而尾部分则是把流S的尾部分
and the rest of it is the result of taking the tail of S,
178
00:10:51,080 --> 00:10:53,728
过滤掉所有
filtering it to throw away all the things
179
00:10:53,744 --> 00:10:55,320
能被S头部分整除的数
that are divisible by the head of S,
180
00:10:56,416 --> 00:10:57,568
然后再对结果筛选
and now sieving the result.
181
00:10:59,020 --> 00:11:00,096
这个代码就是这样
That's just what I did.
182
00:11:01,980 --> 00:11:04,688
现在 为了得到由质数构成的无穷流
And now to get the infinite stream of times,
183
00:11:05,024 --> 00:11:06,900
我们对从2开始的整数流进行SIEVE
we just sieve all the integers starting from 2.
184
00:11:14,920 --> 00:11:15,568
我们来实践一下
Let's try that.
185
00:11:16,300 --> 00:11:18,304
实际上 我们可以在计算机中运行
We can actually do it.
186
00:11:19,760 --> 00:11:22,128
我希望我已经预先输入过SIEVE的定义了
I typed in the definition of sieve before, I hope,
187
00:11:22,864 --> 00:11:24,064
所以我可以定义
so I can say something like
188
00:11:24,928 --> 00:11:33,456
我可以把PRIMES定义为
define the primes to be
189
00:11:34,640 --> 00:11:41,456
(SIEVE (INTEGERS-FROM 2))
the result of sieving the integers starting with 2.
190
00:11:46,760 --> 00:11:48,100
现在我就得到了质数构成的表
So now I've got this list of primes.
191
00:11:48,100 --> 00:11:50,990
这样就得到了所有的质数 对吧?
That's all of the primes, right?
192
00:11:50,990 --> 00:11:53,520
比如我可以问 第20个质数是什么?
So, if for example, what's the 20th prime in that list?
193
00:12:00,736 --> 00:12:01,680
结果是73
73.
194
00:12:02,540 --> 00:12:03,344
那个短促的停顿
See, and that little pause,
195
00:12:03,360 --> 00:12:04,928
这是因为
it was only at the point
196
00:12:04,940 --> 00:12:06,432
在我询问第20个元素时
when I started asking for the 20th prime
197
00:12:06,464 --> 00:12:07,680
它才进行实际的计算
is that it started computing.
198
00:12:10,370 --> 00:12:11,296
在这里 我也可以要求
Or I can say here
199
00:12:13,808 --> 00:12:14,880
打印所有的质数
Or I can say here let's look at all of the primes.
200
00:12:22,640 --> 00:12:24,400
解释器就开始计算并打印所有的质数
And there it goes computing all of the primes.
201