forked from DeathKing/Learning-SICP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec6a_512kb.mp4.srt
5752 lines (4434 loc) · 89.9 KB
/
lec6a_512kb.mp4.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
0
00:00:00,000 --> 00:00:18,550
1
00:00:18,550 --> 00:00:21,230
PROFESSOR: Well, last time Gerry
really let the cat out
2
00:00:21,230 --> 00:00:22,230
of the bag.
3
00:00:22,230 --> 00:00:26,350
He introduced the idea
of assignment.
4
00:00:26,350 --> 00:00:33,405
Assignment and state.
5
00:00:33,405 --> 00:00:37,620
6
00:00:37,620 --> 00:00:41,500
And as we started to see, the
implications of introducing
7
00:00:41,500 --> 00:00:43,860
assignment and state into the
language are absolutely
8
00:00:43,860 --> 00:00:45,350
frightening.
9
00:00:45,350 --> 00:00:47,240
First of all, the substitution
model of
10
00:00:47,240 --> 00:00:48,865
evaluation breaks down.
11
00:00:48,865 --> 00:00:52,210
And we have to use this much
more complicated environment
12
00:00:52,210 --> 00:00:53,780
model and this very mechanistic
thing with
13
00:00:53,780 --> 00:00:56,530
diagrams, even to say what
statements in the programming
14
00:00:56,530 --> 00:00:58,130
language mean.
15
00:00:58,130 --> 00:01:00,260
And that's not a mere
technical point.
16
00:01:00,260 --> 00:01:03,090
See, it's not that we had this
particular substitution model
17
00:01:03,090 --> 00:01:05,200
and, well, it doesn't quite
work, so we have to do
18
00:01:05,200 --> 00:01:05,870
something else.
19
00:01:05,870 --> 00:01:10,730
It's that nothing like the
substitution model can work.
20
00:01:10,730 --> 00:01:15,950
Because suddenly, a variable
is not just something that
21
00:01:15,950 --> 00:01:18,080
stands for a value.
22
00:01:18,080 --> 00:01:22,390
A variable now has to somehow
specify a place
23
00:01:22,390 --> 00:01:23,630
that holds a value.
24
00:01:23,630 --> 00:01:25,885
And the value that's in
that place can change.
25
00:01:25,885 --> 00:01:30,280
26
00:01:30,280 --> 00:01:39,110
Or for instance, an expression
like f of x might have a side
27
00:01:39,110 --> 00:01:40,410
effect in it.
28
00:01:40,410 --> 00:01:44,160
So if we say f of x and it has
some value, and then later we
29
00:01:44,160 --> 00:01:48,890
say f of x again, we might
get a different value
30
00:01:48,890 --> 00:01:49,730
depending on the order.
31
00:01:49,730 --> 00:01:52,780
So suddenly, we have to think
not only about values but
32
00:01:52,780 --> 00:01:54,030
about time.
33
00:01:54,030 --> 00:01:57,970
34
00:01:57,970 --> 00:02:02,070
And then things like pairs are
no longer just their CARs and
35
00:02:02,070 --> 00:02:02,520
their CDRs.
36
00:02:02,520 --> 00:02:06,310
A pair now is not quite its CAR
and its CDR. It's rather
37
00:02:06,310 --> 00:02:08,449
its identity.
38
00:02:08,449 --> 00:02:11,650
So a pair has identity.
39
00:02:11,650 --> 00:02:12,900
It's an object.
40
00:02:12,900 --> 00:02:21,330
41
00:02:21,330 --> 00:02:26,280
And two pairs that have the same
CAR and CDR might be the
42
00:02:26,280 --> 00:02:29,650
same or different, because
suddenly we have to worry
43
00:02:29,650 --> 00:02:30,900
about sharing.
44
00:02:30,900 --> 00:02:34,960
45
00:02:34,960 --> 00:02:38,910
So all of these things enter
as soon as we introduce
46
00:02:38,910 --> 00:02:40,480
assignment.
47
00:02:40,480 --> 00:02:43,340
See, this is a really far cry
from where we started with
48
00:02:43,340 --> 00:02:45,400
substitution.
49
00:02:45,400 --> 00:02:50,420
It's a technically harder way
of looking at things because
50
00:02:50,420 --> 00:02:52,710
we have to think more
mechanistically about our
51
00:02:52,710 --> 00:02:53,540
programming language.
52
00:02:53,540 --> 00:02:55,960
We can't just think about
it as mathematics.
53
00:02:55,960 --> 00:02:59,860
It's philosophically harder,
because suddenly there are all
54
00:02:59,860 --> 00:03:02,020
these funny issues about what
does it mean that something
55
00:03:02,020 --> 00:03:04,050
changes or that two things
are the same.
56
00:03:04,050 --> 00:03:07,910
And also, it's programming
harder, because as Gerry
57
00:03:07,910 --> 00:03:10,070
showed last time, there are all
these bugs having to do
58
00:03:10,070 --> 00:03:14,420
with bad sequencing and aliasing
that just don't exist
59
00:03:14,420 --> 00:03:18,210
in a language where we don't
worry about objects.
60
00:03:18,210 --> 00:03:23,635
Well, how'd we get
into this mess?
61
00:03:23,635 --> 00:03:27,500
Remember what we did, the reason
we got into this is
62
00:03:27,500 --> 00:03:35,750
because we were looking to
build modular systems. We
63
00:03:35,750 --> 00:03:40,250
wanted to build systems that
fall apart into chunks that
64
00:03:40,250 --> 00:03:42,760
seem natural.
65
00:03:42,760 --> 00:03:46,260
So for instance, we want to take
a random number generator
66
00:03:46,260 --> 00:03:48,660
and package up the state of that
random number generator
67
00:03:48,660 --> 00:03:52,840
inside of it so that we can
separate the idea of picking
68
00:03:52,840 --> 00:03:56,640
random numbers from the general
Monte Carlo strategy
69
00:03:56,640 --> 00:03:59,740
of estimating something and
separate that from the
70
00:03:59,740 --> 00:04:03,060
particular way that you work
with random numbers in that
71
00:04:03,060 --> 00:04:06,980
formula developed by
Cesaro for pi.
72
00:04:06,980 --> 00:04:11,400
And similarly, when we go off
and construct some models of
73
00:04:11,400 --> 00:04:15,440
things, if we go off and model
a system that we see in the
74
00:04:15,440 --> 00:04:19,050
real world, we'd like our
program to break into natural
75
00:04:19,050 --> 00:04:22,310
pieces, pieces that mirror the
parts of the system that we
76
00:04:22,310 --> 00:04:24,900
see in the real world.
77
00:04:24,900 --> 00:04:28,780
So for example, if we look at
a digital circuit, we say,
78
00:04:28,780 --> 00:04:33,910
gee, there's a circuit and
it has a piece and
79
00:04:33,910 --> 00:04:35,160
it has another piece.
80
00:04:35,160 --> 00:04:40,100
81
00:04:40,100 --> 00:04:43,580
And these different pieces
sort of have identity.
82
00:04:43,580 --> 00:04:45,550
They have state.
83
00:04:45,550 --> 00:04:48,580
And the state sits
on these wires.
84
00:04:48,580 --> 00:04:51,020
And we think of this piece as
an object that's different
85
00:04:51,020 --> 00:04:52,610
from that as an object.
86
00:04:52,610 --> 00:04:54,400
And when we watch the system
change, we think about a
87
00:04:54,400 --> 00:04:57,500
signal coming in here and
changing a state that might be
88
00:04:57,500 --> 00:04:59,860
here and going here and
interacting with a state that
89
00:04:59,860 --> 00:05:02,170
might be stored there,
and so on and so on.
90
00:05:02,170 --> 00:05:06,860
91
00:05:06,860 --> 00:05:12,760
So what we'd like is we'd like
to build in the computer
92
00:05:12,760 --> 00:05:17,340
systems that fall into pieces
that mirror our view of
93
00:05:17,340 --> 00:05:19,870
reality, of the way that the
actual systems we're modeling
94
00:05:19,870 --> 00:05:23,365
seem to fall into pieces.
95
00:05:23,365 --> 00:05:28,970
Well, maybe the reason that
building systems like this
96
00:05:28,970 --> 00:05:31,600
seems to introduce such
technical complications has
97
00:05:31,600 --> 00:05:33,610
nothing to do with computers.
98
00:05:33,610 --> 00:05:37,960
See, maybe the real reason that
we pay such a price to
99
00:05:37,960 --> 00:05:41,910
write programs that mirror our
view of reality is that we
100
00:05:41,910 --> 00:05:44,550
have the wrong view
of reality.
101
00:05:44,550 --> 00:05:47,460
See, maybe time is just
an illusion, and
102
00:05:47,460 --> 00:05:50,150
nothing ever changes.
103
00:05:50,150 --> 00:05:52,910
See, for example, if I take this
chalk, and we say, gee,
104
00:05:52,910 --> 00:05:55,820
this is an object and
it has a state.
105
00:05:55,820 --> 00:05:59,710
At each moment it has a position
and a velocity.
106
00:05:59,710 --> 00:06:01,240
And if we do something,
that state can change.
107
00:06:01,240 --> 00:06:04,340
108
00:06:04,340 --> 00:06:07,900
But if you studied any
relativity, for instance, you
109
00:06:07,900 --> 00:06:09,760
know that you don't think of
the path of that chalk as
110
00:06:09,760 --> 00:06:11,340
something that goes on
instant by instant.
111
00:06:11,340 --> 00:06:13,870
It's more insightful to think
of that whole chalk's
112
00:06:13,870 --> 00:06:16,020
existence as a path
in space-time.
113
00:06:16,020 --> 00:06:18,040
that's all splayed out.
114
00:06:18,040 --> 00:06:19,840
There aren't individual
positions and velocities.
115
00:06:19,840 --> 00:06:24,640
There's just its unchanging
existence in space-time.
116
00:06:24,640 --> 00:06:28,080
Similarly, if we look at this
electrical system, if we
117
00:06:28,080 --> 00:06:32,450
imagine this electrical system
is implementing some sort of
118
00:06:32,450 --> 00:06:35,730
signal processing system, the
signal processing engineer who
119
00:06:35,730 --> 00:06:39,010
put that thing together doesn't
think of it as, well,
120
00:06:39,010 --> 00:06:41,490
at each instance there's
a voltage coming in.
121
00:06:41,490 --> 00:06:43,340
And that translates
into something.
122
00:06:43,340 --> 00:06:46,400
And that affects the state over
here, which changes the
123
00:06:46,400 --> 00:06:46,810
state over here.
124
00:06:46,810 --> 00:06:49,060
Nobody putting together a
signal processing system
125
00:06:49,060 --> 00:06:50,420
thinks about it like that.
126
00:06:50,420 --> 00:06:56,830
Instead, you say there's
this signal that's
127
00:06:56,830 --> 00:06:58,060
splayed out over time.
128
00:06:58,060 --> 00:07:01,100
And if this is acting as a
filter, this whole thing
129
00:07:01,100 --> 00:07:09,570
transforms this whole thing for
some sort of other output.
130
00:07:09,570 --> 00:07:11,790
You don't think of it as what's
happening instant by
131
00:07:11,790 --> 00:07:14,160
instant as the state
of these things.
132
00:07:14,160 --> 00:07:17,990
And somehow you think of this
box as a whole thing, not as
133
00:07:17,990 --> 00:07:20,980
little pieces sending messages
of state to each other at
134
00:07:20,980 --> 00:07:22,230
particular instants.
135
00:07:22,230 --> 00:07:28,250
136
00:07:28,250 --> 00:07:30,130
Well, today we're going to
look at another way to
137
00:07:30,130 --> 00:07:34,260
decompose systems that's more
like the signal processing
138
00:07:34,260 --> 00:07:37,050
engineer's view of the world
than it is like thinking about
139
00:07:37,050 --> 00:07:41,130
objects that communicate
sending messages.
140
00:07:41,130 --> 00:07:43,310
That's called stream
processing.
141
00:07:43,310 --> 00:07:54,570
142
00:07:54,570 --> 00:08:01,790
And we're going to start by
showing how we can make our
143
00:08:01,790 --> 00:08:08,550
programs more uniform and see
a lot more commonality if we
144
00:08:08,550 --> 00:08:12,490
throw out of these programs
what you might say is an
145
00:08:12,490 --> 00:08:17,210
inordinate concern with
worrying about time.
146
00:08:17,210 --> 00:08:19,910
Let me start by comparing
two procedures.
147
00:08:19,910 --> 00:08:23,260
148
00:08:23,260 --> 00:08:25,690
The first one does this.
149
00:08:25,690 --> 00:08:27,770
We imagine that there's
a tree.
150
00:08:27,770 --> 00:08:30,400
151
00:08:30,400 --> 00:08:33,179
Say there's a tree
of integers.
152
00:08:33,179 --> 00:08:34,429
It's a binary tree.
153
00:08:34,429 --> 00:08:39,100
154
00:08:39,100 --> 00:08:40,230
So it looks like this.
155
00:08:40,230 --> 00:08:44,990
And there's integers in
each of the nodes.
156
00:08:44,990 --> 00:08:51,000
And what we would like to
compute is for each odd number
157
00:08:51,000 --> 00:08:54,210
sitting here, we'd like to find
the square and then sum
158
00:08:54,210 --> 00:08:57,210
up all those squares.
159
00:08:57,210 --> 00:08:59,480
Well, that should be a familiar
kind of thing.
160
00:08:59,480 --> 00:09:02,930
There's a recursive strategy
for doing it.
161
00:09:02,930 --> 00:09:04,880
We look at each leaf, and
either it's going to
162
00:09:04,880 --> 00:09:06,690
contribute the square of
the number if it's odd
163
00:09:06,690 --> 00:09:08,680
or 0 if it's even.
164
00:09:08,680 --> 00:09:13,280
And then recursively, we can say
at each tree, the sum of
165
00:09:13,280 --> 00:09:15,330
all of them is the sum coming
from the right branch and the
166
00:09:15,330 --> 00:09:17,640
left branch, and recursively
down through the nodes.
167
00:09:17,640 --> 00:09:20,360
And that's a familiar way of
thinking about programming.
168
00:09:20,360 --> 00:09:23,960
Let's actually look at
that on the slide.
169
00:09:23,960 --> 00:09:27,960
We say to sum the odd squares
in a tree, well, there's a
170
00:09:27,960 --> 00:09:30,520
test. Either it's a leaf node,
and we're going to check to
171
00:09:30,520 --> 00:09:34,710
see if it's an integer, and then
either it's odd, in which
172
00:09:34,710 --> 00:09:37,160
we take the square,
or else it's 0.
173
00:09:37,160 --> 00:09:40,260
And then the sum of the whole
thing is the sum coming from
174
00:09:40,260 --> 00:09:42,120
the left branch and
the right branch.
175
00:09:42,120 --> 00:09:46,340
176
00:09:46,340 --> 00:09:51,560
OK, well, let me contrast that
with a second problem.
177
00:09:51,560 --> 00:09:55,810
Suppose I give you an integer
n, and then some function to
178
00:09:55,810 --> 00:09:59,270
compute of the first of each
integer in 1 through n.
179
00:09:59,270 --> 00:10:01,810
And then I want to collect
together in a list all those
180
00:10:01,810 --> 00:10:05,600
function values that satisfy
some property.
181
00:10:05,600 --> 00:10:06,880
That's a general
kind of thing.
182
00:10:06,880 --> 00:10:09,750
Let's say to be specific, let's
imagine that for each
183
00:10:09,750 --> 00:10:11,270
integer, k, we're
going to compute
184
00:10:11,270 --> 00:10:14,210
the k Fibonacci number.
185
00:10:14,210 --> 00:10:17,550
And then we'll see which of
those are odd and assemble
186
00:10:17,550 --> 00:10:19,050
those into a list.
187
00:10:19,050 --> 00:10:20,710
So here's a procedure
that does that.
188
00:10:20,710 --> 00:10:23,730
189
00:10:23,730 --> 00:10:26,240
Find the odd Fibonacci numbers
among the first n.
190
00:10:26,240 --> 00:10:28,910
And here is a standard loop the
way we've been writing it.
191
00:10:28,910 --> 00:10:30,800
This is a recursion.
192
00:10:30,800 --> 00:10:33,740
It's a loop on k, and says if
k is bigger than n, it's the
193
00:10:33,740 --> 00:10:36,990
empty list. Otherwise we compute
the k-th Fibonacci
194
00:10:36,990 --> 00:10:40,370
number, call that f.
195
00:10:40,370 --> 00:10:45,180
If it's odd, we CONS it on
to the list starting
196
00:10:45,180 --> 00:10:47,690
with the next one.
197
00:10:47,690 --> 00:10:50,390
And otherwise, we just
take the next one.
198
00:10:50,390 --> 00:10:52,000
And this is the standard
way we've been
199
00:10:52,000 --> 00:10:53,000
writing iterative loops.
200
00:10:53,000 --> 00:10:57,600
And we start off calling
that loop with 1.
201
00:10:57,600 --> 00:11:01,600
OK, so there are
two procedures.
202
00:11:01,600 --> 00:11:02,900
Those procedures look
very different.
203
00:11:02,900 --> 00:11:04,390
They have very different
structures.
204
00:11:04,390 --> 00:11:07,740
Yet from a certain point of
view, those procedures are
205
00:11:07,740 --> 00:11:11,330
really doing very much
the same thing.
206
00:11:11,330 --> 00:11:14,930
So if I was talking like a
signal processing engineer,
207
00:11:14,930 --> 00:11:25,730
what I might say is that the
first procedure enumerates the
208
00:11:25,730 --> 00:11:26,980
leaves of a tree.
209
00:11:26,980 --> 00:11:31,160
210
00:11:31,160 --> 00:11:33,510
And then we can think of a
signal coming out of that,
211
00:11:33,510 --> 00:11:35,330
which is all the leaves.
212
00:11:35,330 --> 00:11:43,970