forked from DeathKing/Learning-SICP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec7a_512kb.mp4.srt
6556 lines (4967 loc) · 100 KB
/
lec7a_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:15,314
1
00:00:15,314 --> 00:00:17,580
PROFESSOR: Well today we're
going to learn about something
2
00:00:17,580 --> 00:00:18,410
quite amazing.
3
00:00:18,410 --> 00:00:22,950
We're going to understand what
we mean by a program a little
4
00:00:22,950 --> 00:00:26,800
bit more profoundly than
we have up till now.
5
00:00:26,800 --> 00:00:30,650
Up till now, we've been thinking
of programs as
6
00:00:30,650 --> 00:00:32,729
describing machines.
7
00:00:32,729 --> 00:00:38,800
So for example, looking at this
still store, we see here
8
00:00:38,800 --> 00:00:42,800
is a program for factorial.
9
00:00:42,800 --> 00:00:46,970
And what it is, is a character
string description, if you
10
00:00:46,970 --> 00:00:49,520
will, of the wiring
diagram of a
11
00:00:49,520 --> 00:00:52,230
potentially infinite machine.
12
00:00:52,230 --> 00:00:53,870
And we can look at that
a little bit and
13
00:00:53,870 --> 00:00:55,130
just see the idea.
14
00:00:55,130 --> 00:00:58,950
That this is a sort of compact
notation which says, if n is
15
00:00:58,950 --> 00:01:00,170
0, the result is one.
16
00:01:00,170 --> 00:01:03,800
Well here comes n coming into
this machine, and if it's 0,
17
00:01:03,800 --> 00:01:06,720
then I control this switch in
such a way that the switch
18
00:01:06,720 --> 00:01:09,340
allows the output to be one.
19
00:01:09,340 --> 00:01:12,970
Otherwise, it's n times
factorial of n minus one.
20
00:01:12,970 --> 00:01:15,920
Well, I'm computing factorial of
n minus one and multiplying
21
00:01:15,920 --> 00:01:19,350
that by n, and, in the case that
it's not 0, this switch
22
00:01:19,350 --> 00:01:21,900
makes the output come
from there.
23
00:01:21,900 --> 00:01:24,460
Of course, this is a machine
with a potentially infinite
24
00:01:24,460 --> 00:01:27,300
number of parts, because
factorial occurs within
25
00:01:27,300 --> 00:01:31,070
factorial, so we don't know
how deep it has to be.
26
00:01:31,070 --> 00:01:36,480
But that's basically what our
notation for programs really
27
00:01:36,480 --> 00:01:38,310
means to us at this point.
28
00:01:38,310 --> 00:01:41,810
It's a character string
description, if you will, of a
29
00:01:41,810 --> 00:01:44,900
wiring diagram that could also
be drawn some other way.
30
00:01:44,900 --> 00:01:47,520
And, in fact, many people have
proposed to me, programming
31
00:01:47,520 --> 00:01:49,490
languages look graphical
like this.
32
00:01:49,490 --> 00:01:51,500
I'm not sure I believe there
are many advantages.
33
00:01:51,500 --> 00:01:54,470
The major disadvantage, of
course, is that it takes up
34
00:01:54,470 --> 00:01:57,360
more space on a page, and,
therefore, it's harder to pack
35
00:01:57,360 --> 00:02:01,090
into a listing or to
edit very well.
36
00:02:01,090 --> 00:02:05,300
But in any case, there's
something very remarkable that
37
00:02:05,300 --> 00:02:08,810
can happen in the competition
world which is that you can
38
00:02:08,810 --> 00:02:10,450
have something called
a universal machine.
39
00:02:10,450 --> 00:02:18,340
If we look at the second
slide, what we see is a
40
00:02:18,340 --> 00:02:21,260
special machine called eval.
41
00:02:21,260 --> 00:02:23,670
There is a machine called eval,
and I'm going to show it
42
00:02:23,670 --> 00:02:25,720
to you today.
43
00:02:25,720 --> 00:02:27,780
It's very simple.
44
00:02:27,780 --> 00:02:30,490
What is remarkable is that it
will fit on the blackboard.
45
00:02:30,490 --> 00:02:33,350
46
00:02:33,350 --> 00:02:38,310
However, eval is a machine
which takes as input a
47
00:02:38,310 --> 00:02:40,450
description of another
machine.
48
00:02:40,450 --> 00:02:42,620
It could take the wiring
diagram of a
49
00:02:42,620 --> 00:02:46,490
factorial machine as input.
50
00:02:46,490 --> 00:02:52,020
Having done so, it becomes a
simulator for the factorial
51
00:02:52,020 --> 00:02:58,910
machine such that, if you put
a six in, out comes a 720.
52
00:02:58,910 --> 00:03:02,130
That's a very remarkable
sort of machine.
53
00:03:02,130 --> 00:03:04,560
And the most amazing part of
it is that it fits on a
54
00:03:04,560 --> 00:03:05,590
blackboard.
55
00:03:05,590 --> 00:03:10,070
By contrast, one could imagine
in the analog electronics
56
00:03:10,070 --> 00:03:17,180
world a very different machine,
a machine which also
57
00:03:17,180 --> 00:03:20,440
was, in some sense, universal,
where you gave a circuit
58
00:03:20,440 --> 00:03:24,830
diagram as one of the inputs,
for example, of this little
59
00:03:24,830 --> 00:03:28,050
low-pass filter, one-pole
low-pass filter.
60
00:03:28,050 --> 00:03:30,230
And you can imagine that
you could, for
61
00:03:30,230 --> 00:03:32,030
example, scan this out--
62
00:03:32,030 --> 00:03:37,950
the scan lines are the signal
that's describing what this
63
00:03:37,950 --> 00:03:40,770
machine is to simulate--
64
00:03:40,770 --> 00:03:43,040
then the analog of that which
is made out of electrical
65
00:03:43,040 --> 00:03:45,540
circuits, should configure
itself into a filter that has
66
00:03:45,540 --> 00:03:47,010
the frequency response
specified
67
00:03:47,010 --> 00:03:49,890
by the circuit diagram.
68
00:03:49,890 --> 00:03:52,520
That's a very hard machine to
make, and, surely, there's no
69
00:03:52,520 --> 00:03:55,670
chance that I could put
it on a blackboard.
70
00:03:55,670 --> 00:03:58,430
So we're going to see an
amazing thing today.
71
00:03:58,430 --> 00:04:01,240
We're going to see,
on the blackboard,
72
00:04:01,240 --> 00:04:02,790
the universal machine.
73
00:04:02,790 --> 00:04:06,780
And we'll see that among other
things, it's extremely simple.
74
00:04:06,780 --> 00:04:10,070
Now, we're getting very close
to the real spirit in the
75
00:04:10,070 --> 00:04:11,280
computer at this point.
76
00:04:11,280 --> 00:04:14,110
So I have to show a certain
amount of reverence and
77
00:04:14,110 --> 00:04:16,970
respect, so I'm going to wear
a suit jacket for the only
78
00:04:16,970 --> 00:04:20,470
time that you'll ever see me
wear a suit jacket here.
79
00:04:20,470 --> 00:04:25,730
And I think I'm also going to
put on an appropriate hat for
80
00:04:25,730 --> 00:04:26,980
the occasion.
81
00:04:26,980 --> 00:04:28,780
82
00:04:28,780 --> 00:04:31,390
Now, this is a lecturer which
I have to warn you--
83
00:04:31,390 --> 00:04:34,140
84
00:04:34,140 --> 00:04:37,690
let's see, normally, people
under 40 and who don't have
85
00:04:37,690 --> 00:04:40,370
several children are advised
to be careful.
86
00:04:40,370 --> 00:04:44,170
If they're really worried, they
should leave. Because
87
00:04:44,170 --> 00:04:46,890
there's a certain amount of
mysticism that will appear
88
00:04:46,890 --> 00:04:50,140
here which may be disturbing
and cause
89
00:04:50,140 --> 00:04:51,820
trouble in your minds.
90
00:04:51,820 --> 00:04:57,300
Well in any case, let's see,
I wish to write for you the
91
00:04:57,300 --> 00:05:02,510
evaluator for Lisp.
92
00:05:02,510 --> 00:05:05,020
Now the evaluator isn't
very complicated.
93
00:05:05,020 --> 00:05:08,240
It's very much like all the
programs we've seen already.
94
00:05:08,240 --> 00:05:10,860
That's the amazing part of it.
95
00:05:10,860 --> 00:05:15,370
It's going to be-- and I'm going
to write it right here--
96
00:05:15,370 --> 00:05:16,620
it's a program called eval.
97
00:05:16,620 --> 00:05:22,900
98
00:05:22,900 --> 00:05:28,780
And it's a procedure of two
arguments in expression of an
99
00:05:28,780 --> 00:05:30,030
environment.
100
00:05:30,030 --> 00:05:31,860
101
00:05:31,860 --> 00:05:33,130
And like every interesting
102
00:05:33,130 --> 00:05:34,940
procedure, it's a case analysis.
103
00:05:34,940 --> 00:05:40,460
104
00:05:40,460 --> 00:05:44,210
But before I start on this, I
want to tell you some things.
105
00:05:44,210 --> 00:05:46,880
The program we're going to write
on the blackboard is
106
00:05:46,880 --> 00:05:52,450
ugly, dirty, disgusting, not the
way I would write this is
107
00:05:52,450 --> 00:05:54,210
a professional.
108
00:05:54,210 --> 00:05:57,940
It is written with concrete
syntax, meaning you've got
109
00:05:57,940 --> 00:05:59,630
really to use lots of CARs and
CDRs which is exactly what I
110
00:05:59,630 --> 00:06:02,550
told you not to do.
111
00:06:02,550 --> 00:06:07,180
That's on purpose in this case,
because I want it to be
112
00:06:07,180 --> 00:06:11,010
small, compact, fit on the
blackboard so you can get the
113
00:06:11,010 --> 00:06:12,420
whole thing.
114
00:06:12,420 --> 00:06:15,800
So I don't want to use long
names like I normally use.
115
00:06:15,800 --> 00:06:19,580
I want to use CAR-CDR
because it's short.
116
00:06:19,580 --> 00:06:20,950
Now, that's a trade-off.
117
00:06:20,950 --> 00:06:23,570
I don't want you writing
programs like this.
118
00:06:23,570 --> 00:06:26,090
This is purely for an effect.
119
00:06:26,090 --> 00:06:27,530
Now, you're going to have to
work a little harder to read
120
00:06:27,530 --> 00:06:29,270
it, but I'm going to try
to make it clear
121
00:06:29,270 --> 00:06:31,270
as I'm writing it.
122
00:06:31,270 --> 00:06:32,395
I'm also--
123
00:06:32,395 --> 00:06:34,960
this is a pretty much complete
interpreter, but there's going
124
00:06:34,960 --> 00:06:36,290
to be room for putting
in more things--
125
00:06:36,290 --> 00:06:39,160
I'm going to leave out
definition and assignment,
126
00:06:39,160 --> 00:06:45,310
just because they are not
essential, for a mathematical
127
00:06:45,310 --> 00:06:51,670
reason I'll show you later and
also they take up more space.
128
00:06:51,670 --> 00:06:54,170
But, in any case, what
do we have to do?
129
00:06:54,170 --> 00:06:57,160
We have to do a dispatch which
breaks the types of
130
00:06:57,160 --> 00:07:02,030
expressions up into particular
classes.
131
00:07:02,030 --> 00:07:03,525
So that's what we're
going to have here.
132
00:07:03,525 --> 00:07:05,150
Well, what expressions
are there?
133
00:07:05,150 --> 00:07:06,810
Let's look at the kinds
of expressions.
134
00:07:06,810 --> 00:07:10,420
We can have things like
the numeral three.
135
00:07:10,420 --> 00:07:12,720
What do I want that to do?
136
00:07:12,720 --> 00:07:15,640
I can make choices, but I think
right now, I want it to
137
00:07:15,640 --> 00:07:17,050
be a three.
138
00:07:17,050 --> 00:07:18,860
That's what I want.
139
00:07:18,860 --> 00:07:19,800
So that's easy enough.
140
00:07:19,800 --> 00:07:27,520
That means I want, if the
thing is a number, the
141
00:07:27,520 --> 00:07:30,720
expression, that I want
the expression
142
00:07:30,720 --> 00:07:31,970
itself as the answer.
143
00:07:31,970 --> 00:07:35,420
144
00:07:35,420 --> 00:07:37,700
Now the next possibility
is things that we
145
00:07:37,700 --> 00:07:39,390
represent as symbols.
146
00:07:39,390 --> 00:07:47,614
Examples of symbols are things
like x, n, eval, number, x.
147
00:07:47,614 --> 00:07:49,630
What do I mean them to be?
148
00:07:49,630 --> 00:07:51,690
Those are things that stand
for other things.
149
00:07:51,690 --> 00:07:54,770
Those are the variables
of our language.
150
00:07:54,770 --> 00:07:58,540
And so I want to be able to say,
for example, that x, for
151
00:07:58,540 --> 00:08:02,930
example, transforms to it's
value which might be three.
152
00:08:02,930 --> 00:08:07,920
Or I might ask something
like car.
153
00:08:07,920 --> 00:08:09,710
I want to have as its value--
154
00:08:09,710 --> 00:08:17,380
be something like some
procedure, which I don't know
155
00:08:17,380 --> 00:08:20,440
what is inside there, perhaps
a machine language code or
156
00:08:20,440 --> 00:08:23,100
something like that.
157
00:08:23,100 --> 00:08:24,430
So, well, that's easy enough.
158
00:08:24,430 --> 00:08:27,890
I'm going to push that
off on someone else.
159
00:08:27,890 --> 00:08:33,370
If something is a symbol, if
the expression is a symbol,
160
00:08:33,370 --> 00:08:38,140
then I want the answer to be
the result, looking up the
161
00:08:38,140 --> 00:08:40,159
expression in the environment.
162
00:08:40,159 --> 00:08:46,480
163
00:08:46,480 --> 00:08:52,410
Now the environment is a
dictionary which maps the
164
00:08:52,410 --> 00:08:54,060
symbol names to their values.
165
00:08:54,060 --> 00:08:56,280
And that's all it is.
166
00:08:56,280 --> 00:08:57,530
How it's done?
167
00:08:57,530 --> 00:08:59,760
Well, we'll see that later.
168
00:08:59,760 --> 00:09:01,670
It's very easy.
169
00:09:01,670 --> 00:09:03,630
It's easy to make data
structures that are tables of
170
00:09:03,630 --> 00:09:04,670
various sorts.
171
00:09:04,670 --> 00:09:07,080
But it's only a table, and this
is the access routine for
172
00:09:07,080 --> 00:09:10,040
some table.
173
00:09:10,040 --> 00:09:12,720
Well, the next thing, another
kind of expression--
174
00:09:12,720 --> 00:09:14,870
you have things that are
described constants that are
175
00:09:14,870 --> 00:09:17,430
not numbers, like 'foo.
176
00:09:17,430 --> 00:09:20,170
177
00:09:20,170 --> 00:09:22,450
Well, for my convenience,
I want to syntactically
178
00:09:22,450 --> 00:09:31,520
transform that into a list
structure which is, quote foo.
179
00:09:31,520 --> 00:09:35,140
180
00:09:35,140 --> 00:09:39,950
A quoted object, whatever it is,
is going to be actually an
181
00:09:39,950 --> 00:09:43,550
abbreviation, which is not
part of the evaluator but
182
00:09:43,550 --> 00:09:46,960
happens somewhere else, an
abbreviation for an expression
183
00:09:46,960 --> 00:09:48,780
that looks like this.
184
00:09:48,780 --> 00:09:52,120
This way, I can test for the
type of the expression as
185
00:09:52,120 --> 00:09:55,615
being a quotation by examining
the car of the expression.
186
00:09:55,615 --> 00:09:58,460
187
00:09:58,460 --> 00:10:01,650
So I'm not going to worry about
that in the evaluator.
188
00:10:01,650 --> 00:10:02,780
It's happening somewhere
earlier in
189
00:10:02,780 --> 00:10:05,540
the reader or something.
190
00:10:05,540 --> 00:10:18,620
If the expression of the
expression is quote, then what
191
00:10:18,620 --> 00:10:25,140
I want, I want quote foo to
itself evaluate to foo.
192
00:10:25,140 --> 00:10:27,530
It's a constant.
193
00:10:27,530 --> 00:10:30,645
This is just a way of saying
that this evaluates to itself.
194
00:10:30,645 --> 00:10:33,150
195
00:10:33,150 --> 00:10:33,660
What is that?
196
00:10:33,660 --> 00:10:37,330
That's the second of the list.
It's the second element of the
197
00:10:37,330 --> 00:10:41,604
list. The second element of the
list is it's CADR. So I'm
198
00:10:41,604 --> 00:10:51,290
just going to write
here, CADR.
199
00:10:51,290 --> 00:10:52,510
What else do we have here?
200
00:10:52,510 --> 00:10:56,040
We have lambda expressions,
for example,
201
00:10:56,040 --> 00:11:04,160
lambda of x plus x y.
202
00:11:04,160 --> 00:11:05,910
Well, I going have to have some
representation for the
203
00:11:05,910 --> 00:11:08,610
procedure which is the value of
an expression, of a lambda
204
00:11:08,610 --> 00:11:09,600
expression.
205
00:11:09,600 --> 00:11:13,030
The procedure here is not
the expression lambda x.
206
00:11:13,030 --> 00:11:16,170
That's the description of it,
the textual description.
207
00:11:16,170 --> 00:11:18,800
However, what what I going
to expect to see here is
208
00:11:18,800 --> 00:11:20,930
something which contains an
environment as one of its
209
00:11:20,930 --> 00:11:27,360
parts if I'm implementing
a lexical language.
210
00:11:27,360 --> 00:11:30,790
And so what I'd like to see
is some type flags.
211
00:11:30,790 --> 00:11:33,440
I'm going to have to be able
to distinguish procedures
212
00:11:33,440 --> 00:11:37,190
later, procedures which were
produced by lambdas, from ones
213
00:11:37,190 --> 00:11:39,060
that may be primitive.
214
00:11:39,060 --> 00:11:42,440
And so I'm going to have some