-
Notifications
You must be signed in to change notification settings - Fork 139
/
emacs-tutor2.html
948 lines (824 loc) · 40.9 KB
/
emacs-tutor2.html
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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Emacs Mini Manual (PART 2) - LISP PRIMER: WHY PARENTHESES MATTER</title>
<!-- 2014-11-20 Thu 14:53 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="generator" content="Org-mode" />
<meta name="author" content="Tu" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center; }
.todo { font-family: monospace; color: red; }
.done { color: green; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.right { margin-left: auto; margin-right: 0px; text-align: right; }
.left { margin-left: 0px; margin-right: auto; text-align: left; }
.center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.right { text-align: center; }
th.left { text-align: center; }
th.center { text-align: center; }
td.right { text-align: right; }
td.left { text-align: left; }
td.center { text-align: center; }
dt { font-weight: bold; }
.footpara:nth-child(2) { display: inline; }
.footpara { display: block; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<link rel="stylesheet" href="./static/worg.css">
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="preamble" class="status">
<!-- Start of StatCounter Code for Default Guide -->
<script type="text/javascript">
var sc_project=9874755;
var sc_invisible=1;
var sc_security="c2028bb7";
var scJsHost = (("https:" == document.location.protocol) ?
"https://secure." : "http://www.");
document.write("<sc"+"ript type='text/javascript' src='" + scJsHost+
"statcounter.com/counter/counter.js'></"+"script>");
</script>
<noscript><div class="statcounter"><a title="hit counter"
href="http://statcounter.com/free-hit-counter/" target="_blank"><img
class="statcounter" src="http://c.statcounter.com/9874755/0/c2028bb7/1/"
alt="hit counter"></a></div></noscript>
<!-- End of StatCounter Code for Default Guide -->
<h2><a href="index.html">Back to Table of Contents</a></h2>
</div>
<div id="content">
<h1 class="title">Emacs Mini Manual (PART 2) - LISP PRIMER: WHY PARENTHESES MATTER</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">Introduction - and why you should learn Lisp</a>
<ul>
<li><a href="#sec-1-1">Before we start</a></li>
<li><a href="#sec-1-2">A bit of history</a></li>
<li><a href="#sec-1-3">Basic syntax and semantic</a></li>
<li><a href="#sec-1-4">Beyond parentheses</a></li>
</ul>
</li>
<li><a href="#sec-2">Syntax error</a>
<ul>
<li><a href="#sec-2-1">Unbalanced parentheses:</a></li>
<li><a href="#sec-2-2">Mini-language syntax error:</a></li>
</ul>
</li>
<li><a href="#sec-3">Semantic error</a></li>
<li><a href="#sec-4">Lisp Machine</a></li>
<li><a href="#sec-5">Conclusion</a></li>
</ul>
</div>
</div>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1"><a id="ID-267fa5b6-b998-42c6-8ec0-382035284873" name="ID-267fa5b6-b998-42c6-8ec0-382035284873"></a>Introduction - and why you should learn Lisp</h2>
<div class="outline-text-2" id="text-1">
<p>
In this section, you can try out code by <code>M-x ielm</code>, then paste code
into the Emacs Lisp interpreter. It's important that you understand
the core ideas. After you finish the sub-sections of this section, we
will play with Emacs Lisp code for customizing Emacs, and it's really
fun to see your Emacs "evolves" gradually.
</p>
</div>
<div id="outline-container-sec-1-1" class="outline-3">
<h3 id="sec-1-1"><a id="ID-24b6798b-24de-45dc-94ce-cd2dddf55255" name="ID-24b6798b-24de-45dc-94ce-cd2dddf55255"></a>Before we start</h3>
<div class="outline-text-3" id="text-1-1">
<p>
Please be aware that although I intentionally emphasize the importance
of parentheses, but <b>I don't mean Lisp is all about parentheses</b>. I
just want to clear beginner's confusion with parentheses.
</p>
<p>
In this chapter, I will introduce to you why language like Lisp exists
and what sets it from other languages, along with basic syntax and
semantic. For deeper learning into Emacs Lisp, many tutorials exist
and you can search easily on the internet. But before all of that, I
want you to understand what and why Lisp differs from others, and why
should you use Lisp; otherwise, it's just learning a different ways of
doing things in a different language and won't motivate you to take
Lisp serious because the mentality of "just another programming
language".
</p>
</div>
</div>
<div id="outline-container-sec-1-2" class="outline-3">
<h3 id="sec-1-2"><a id="ID-71c4974f-42b4-4a29-93bd-b823688ea024" name="ID-71c4974f-42b4-4a29-93bd-b823688ea024"></a>A bit of history</h3>
<div class="outline-text-3" id="text-1-2">
<p>
Lisp has a long history. Lisp was designed by computer scientist <a href="http://en.wikipedia.org/wiki/John_McCarthy_%2528computer_scientist%2529">John
McCarthy</a>. Lisp first appeared in 1958 and Lisp is still with us up to
this day with various dialects: Common Lisp, newLisp, Emacs Lisp, Racket,
Clojure… Lisp is short for (<b>LIS</b>)t (<b>P</b>)rocessing.
</p>
<p>
You can read the history of Lisp until 1979 of Lisp, written by John
McCarthy, the creator of Lisp: <a href="http://www-formal.stanford.edu/jmc/history/lisp/lisp.html">History of Lisp</a>.
</p>
<p>
Another good read about Lisp history, written by Paul Graham: <a href="http://www.paulgraham.com/lisphistory.html">Lisp
History</a>.
</p>
<p>
<a href="http://www.lispworks.com/success-stories/index.html">Success Stories</a>.
</p>
</div>
</div>
<div id="outline-container-sec-1-3" class="outline-3">
<h3 id="sec-1-3"><a id="ID-9a30dadd-d95e-49a9-8690-b6b633d9083d" name="ID-9a30dadd-d95e-49a9-8690-b6b633d9083d"></a>Basic syntax and semantic</h3>
<div class="outline-text-3" id="text-1-3">
<p>
Lisp syntax is inherently simple. At its core, this is all that
required to understand any Lisp:
</p>
<ul class="org-ul">
<li>Anything is a list, it's an atom. Atoms are the same as primitives
in other languages: components that cannot be broken down further
into smaller pieces. This is data.
</li>
<li>Anything is not an atom, it's a list that contains atoms. List is
just anything contained in a pair of parentheses. List can be used
either as code for processing something or as data to be processed.
</li>
<li>Data:
<ul class="org-ul">
<li>Atomic-data, or <b>Atom</b>: It is the same as primitives in other
languages, meaning the most basic construct and indivisible. Atoms
include following basic types, you should try in IELM:
<ul class="org-ul">
<li><b>Number</b>: integer like 1, 2, 3… or floating point numbers like
1.5, 3.14…
</li>
<li><b>String</b>: strings like "a", "string", "this is a string"… Is
also an atom. String is immutable.
</li>
<li><code>NIL</code> or <code>()</code>: NIL means <code>null</code> in other languages. <code>()</code> means
empty list, which is equivalent to <code>NIL</code>. When using with a true
or false test, usch as an if statement, <code>NIL</code> or the empty list
<code>()</code> means the value <code>false</code>; all other non-empty values in Lisp
are true.
</li>
<li><b>Symbol</b>: A symbol is an object with a name. You can think of
symbol as pointer or reference in other language. Each symbol
has 4 components:
<ul class="org-ul">
<li>Print name: symbol's name as string
</li>
<li>Value: value of symbol as variable
</li>
<li>Function: symbol's function definition. A symbol can be both a
variable and a function
</li>
<li>Property-list: list of key-value pairs. You don't need to care
much at this stage.
</li>
</ul>
<p>
Example: the variable <code>buffer-file-name</code> is both a variable and
a function. You can check by using <code>symbol-value</code> and
<code>symbol-function</code> functions:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #707183;">(</span><span style="color: #5e7837;">symbol-value</span> '<span style="color: #7e544f;">buffer-file-name</span><span style="color: #707183;">)</span> <span style="color: #b22222;">;; </span><span style="color: #b22222;">check the value of buffer-file-name; if you try it in IELM,</span>
<span style="color: #b22222;">;; </span><span style="color: #b22222;">the value is NIL because the buffer is not associated with any file</span>
<span style="color: #707183;">(</span><span style="color: #513f78;">symbol-function</span> '<span style="color: #7e544f;">buffer-file-name</span><span style="color: #707183;">)</span> <span style="color: #b22222;">;; </span><span style="color: #b22222;">return the function buffer-file-name; </span>
<span style="color: #b22222;">;; </span><span style="color: #b22222;">IELM should display something like this: #<subr buffer-file-name></span>
</pre>
</div>
</li>
</ul>
</li>
<li>Non-atomic: There's a reason Lisp is called (<b>LIS</b>)t
(<b>P</b>)rocessing. In other languages, list is just another data
type; but in Lisp, because of Lisp syntax, list is really
special. Basically, this is a list:
<div class="org-src-container">
<pre class="src src-emacs-lisp">’<span style="color: #707183;">(</span><span style="color: #78683f;">...</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
A list can hold data, both atomic and non-atomic. That means, a
list can hold a smaller list inside it along with atomic
data. List elements are separated by whitespace.
</p>
</li>
</ul>
</li>
<li>Code: If you want to run code to be executed, here is the syntax:
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #707183;">(</span><span style="color: #78683f;">...</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
What is the difference between code and data? That's right, a single
quote <code>’</code>. Without the quote <code>’</code>, we are creating <b>a list</b> that holds
code for computer to execute. With the quote <code>’</code>, we are creating <b>a
list</b> that holds data, similar to List data structure in Java or C++
to hold data.
</p>
</li>
</ul>
<p>
<span class="underline">Examples</span>:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">’<span style="color: #707183;">(</span><span style="color: #008b8b;">1</span> <span style="color: #008b8b;">2</span> <span style="color: #008b8b;">3</span> <span style="color: #008b8b;">4</span><span style="color: #707183;">)</span> <span style="color: #b22222;">;;</span><span style="color: #b22222;">is a list that holds 4 numbers from 1 to 4</span>
’<span style="color: #707183;">(</span><span style="color: #8b2252;">"aa"</span> <span style="color: #8b2252;">"bb"</span> <span style="color: #8b2252;">"cc"</span> <span style="color: #8b2252;">"dd"</span><span style="color: #707183;">)</span> <span style="color: #b22222;">;;</span><span style="color: #b22222;">is a list that holds 4 strings: "aa", "bb", "cc", "dd"</span>
'<span style="color: #707183;">()</span> <span style="color: #b22222;">;; </span><span style="color: #b22222;">an empty list, which is also an atom</span>
’<span style="color: #707183;">(</span><span style="color: #a020f0;">if</span> <span style="color: #78683f;">a</span> <span style="color: #78683f;">b</span> <span style="color: #7a4f7e;">c</span><span style="color: #707183;">)</span> <span style="color: #b22222;">;; </span><span style="color: #b22222;">a list that hold 4 symbol: if, a, b and c</span>
<span style="color: #707183;">(</span><span style="color: #a020f0;">if</span> <span style="color: #78683f;">a</span> <span style="color: #78683f;">b</span> <span style="color: #7a4f7e;">c</span><span style="color: #707183;">)</span> <span style="color: #b22222;">;; </span><span style="color: #b22222;">is an if expression that executes if..then..else</span>
<span style="color: #b22222;">;; </span><span style="color: #b22222;">logic in common programming languages. Expressions like if are</span>
<span style="color: #b22222;">;; </span><span style="color: #b22222;">called *special form*</span>
’<span style="color: #707183;">(</span><span style="color: #707e4f;">+</span> <span style="color: #008b8b;">1</span> <span style="color: #008b8b;">2</span> <span style="color: #008b8b;">3</span><span style="color: #707183;">)</span> <span style="color: #b22222;">;; </span><span style="color: #b22222;">is a list consists of 4 elements: +, 1, 2 and 3</span>
<span style="color: #707183;">(</span><span style="color: #707e4f;">+</span> <span style="color: #008b8b;">1</span> <span style="color: #008b8b;">2</span> <span style="color: #008b8b;">3</span><span style="color: #707183;">)</span> <span style="color: #b22222;">;; </span><span style="color: #b22222;">is a function call to function "+" (yes, "+" is a function)</span>
</pre>
</div>
<p>
Both code and data in Lisp can be represented using the same format: a
pair of parentheses with items in it: <code>(...)</code>; and it is called a
list. This peculiar property is called <b>homoiconicity</b>, and languages
that have this property is called <b>homoiconic languages</b>. It makes
Lisp so powerful: <b>code can be data, data can be code</b>. It is a reason
why Lisp contains a lot of parentheses.
</p>
<p>
Both code and data are represented as a list underlying, but to
distinguish between list that holds data and list that holds code,
list that holds data is referred simply as list; while list that holds
code is <b>Lisp form</b>. But remember, code and data are lists, and
because of the single representation for both code and data, list is
more special in Lisp than in other languages.
</p>
<p>
It's worth to repeat again: <code>’(...)</code> for creating data and <code>(...)</code>
for creating code; you hold things in <code>’(...)</code> and you process things
in <code>(...)</code>. Easy to remember, right?
</p>
<p>
You may think: "Cool, so what difference can homoiconity make?" Let's
look at an example; this is typical <code>if..then..else</code>:
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #a020f0;">if</span> <span style="color: #707183;">(</span><span style="color: #4f7e67;">condition</span><span style="color: #707183;">)</span> <span style="color: #707183;">{</span>
...<span style="color: #784437;">statements</span>...
<span style="color: #707183;">}</span> <span style="color: #a020f0;">else</span> <span style="color: #707183;">{</span>
...<span style="color: #784437;">statements</span>...
<span style="color: #707183;">}</span>
</pre>
</div>
<p>
How do you change its syntax? For example, you prefer Python
<code>if..then..else</code> syntax, how can we change C <code>if..then..else</code> to
Python <code>if...then...else</code> and write our customized version in C:
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">if</span> <span style="color: #4f7e67;">condition</span>:
...<span style="color: #784437;">statements</span>...
<span style="color: #a020f0;">else</span>:
...<span style="color: #784437;">statements</span>...
</pre>
</div>
<p>
The answer is, it's impossible, even with C macro. With Lisp, this
is entirely possible, except one minor thing: the code must be
treated as data, meaning the entire Python <code>if</code> construct above must
be enclosed within a Lisp form like this:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">'<span style="color: #707183;">(</span><span style="color: #a020f0;">if</span> <span style="color: #707e4f;">condition:</span>
<span style="color: #4f7e67;">...statements</span>
<span style="color: #784437;">else:</span>
<span style="color: #707e4f;">...statements...</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
Lisp still has syntax, but minimal: a pair of parentheses, with things in
in it: <code>(...)</code>, along with the syntax for primitives. For that reason,
it can adapt to any type of syntax programmers can imagine. Notice the
single quote <code>’</code>, signalling that the entire form is data, and need to
be processed to create appropriate code when feed into some processing
function.
</p>
<p>
Now you see why Lisp code has a lot of parentheses. This is how
<b>homoiconicity</b> differs. Without being able to treat code as data, you
cannot bend the language to your own will (well, unless you implement
your own language from scratch). Because Lisp's minimal syntax, you
can create your own language for expressing your own ideas. Using your
own language means you can use your own terms, your own rules, to
write your solutions instead of someone imposes a particular style of
language on you, tell you how to do it even if you prefer another
style. This is why Lisp is so expressive: minimal syntax and follow
the will of programmer.
</p>
<p>
Lisp forms are classified into 3 types:
</p>
<ul class="org-ul">
<li><b>Function form</b>:
Function form is the most common form. Function form is equivalent
to a function call in other languages. If the first element in the
list is a function that exists, that function will be called along
with its arguments. The remaining elements in the list are function
arguments. All arguments are evaluated before the function is called.
<p>
<span class="underline">Example</span>:
</p>
<p>
The list <code>(+ 1 (+ 2 3) (* 3 4) (/ 4 2))</code> is a function call to
function <code>+</code>. Nearly everything in Lisp is a function, even
arithmetic operators like <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>. Before the outer most
list is processed, the inner ones will be processed first. <code>(+ 2 3)</code>
becomes 5, <code>(* 3 4)</code> becomes 12, <code>(/ 4 2)</code> becomes 2; all these
three values will then replace its list in the original function
call to make it become: <code>(+ 1 5 12 2)</code>, and finally function <code>+</code> is
called to produce the final result 20.
</p>
</li>
<li><b>Special form</b>:
Special form has special evaluation rules or special syntax or
both. For example, this is <code>if..then..else</code> in Lisp:
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #707183;">(</span><span style="color: #a020f0;">if</span> <span style="color: #4f7e67;">condition</span> <span style="color: #b22222;">;; </span><span style="color: #b22222;">condition is a valid Lisp form</span>
<span style="color: #783778;">...do</span> <span style="color: #37785e;">something</span> <span style="color: #783778;">if</span> <span style="color: #783778;">true...</span>
<span style="color: #783778;">...do</span> <span style="color: #37785e;">something</span> <span style="color: #783778;">if</span> <span style="color: #37785e;">false...</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
Let's consider the behaviour of <code>if</code>, not just in Lisp but in any
language: if condition (a valid Lisp form) is true, then do
something, else do something if false. For this reason, <code>if</code> cannot
be a function call because <code>condition</code>, <code>true</code> and <code>false</code> are all
evaluated and passed into if, while we want first check <code>condition</code>,
then depend on the outcome of <code>condition</code>, we select a true or false
branch.
</p>
<p>
Most forms in Lisp are functions, except special cases such as
<code>if</code>, <code>and</code>, <code>or</code>… that cannot follow the evaluation rule of a
function. They need their own rules that do not exist in other
forms. That's why they are special.
</p>
</li>
<li><b>Macro form</b>:
Macro form is a function, but different: When you call a macro, the
macro function generated regular Lisp code; the generated code then
is executed. Macro is what makes Lisp so special: it allows Lisp to
have any syntax anyone wishes for. The Python syntax enclosed in a
Lisp form you saw earlier is an example. But now, instead of having
to quote, you won't have to with a macro form. Instead of writing
like this:
<div class="org-src-container">
<pre class="src src-emacs-lisp">'<span style="color: #707183;">(</span><span style="color: #a020f0;">if</span> <span style="color: #707e4f;">condition:</span>
<span style="color: #707e4f;">...statements...</span>
<span style="color: #784437;">else:</span>
<span style="color: #707e4f;">...statements...</span><span style="color: #707183;">)</span>
</pre>
</div>
</li>
</ul>
<p>
You can remove the quote <code>’</code> and treat your Python syntax as part of
Lisp:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #707183;">(</span><span style="color: #a020f0;">if</span> <span style="color: #707e4f;">condition:</span>
<span style="color: #707e4f;">...statements...</span>
<span style="color: #784437;">else:</span>
<span style="color: #707e4f;">...statements...</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
The Python code above is a macro form. Upon calling, the macro will
first transform to a valid Lisp form:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #707183;">(</span><span style="color: #a020f0;">if</span> <span style="color: #4f7e67;">condition</span>
<span style="color: #707e4f;">...statements...</span>
<span style="color: #707e4f;">...statements...</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
Then the transformed code is executed. You can have C for loop, Python if,
Java class…mix up in Lisp if you want. Thanks to the minimal Lisp syntax,
Lisp macro is able to do all of this. Without it, you cannot bend Lisp
to your needs.
</p>
<p>
In reality, <code>’(...)</code> is just a syntactic sugar for special form
<code>(quote ...)</code>. In the end, aside from the atoms, Lisp only has one
syntax: a pair of parentheses and items in it. With Lisp syntax, many
things are easy to do in Lisp, such as generating code as data and
execute it later, both in compile time and runtime. In the end, aside
from the primitives, the only thing that exists in Lisp is a pair of
parentheses, with things in in it: <code>(...)</code>. This is the only syntax,
along with the semantics that depends on context: <i>a function form</i>,
<i>a special form</i> or <i>a macro form</i>; the first item in a form is going
to get executed. That's all you need to remember for using any Lisp.
</p>
<p>
If you are still not convinced with the parentheses, perhaps seasoned
Lispers can:
</p>
<ul class="org-ul">
<li><a href="http://symbo1ics.com/blog/?p=275">Lisp has too many parentheses…</a>
</li>
<li>The above article is inspired by <a href="https://groups.google.com/forum/#!msg/comp.lang.lisp/Nvdio3taI0Q/bn_VqLTaxG4J">this Usenet post</a>, which is worth reading.
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-1-4" class="outline-3">
<h3 id="sec-1-4"><a id="ID-e06ce975-464b-495e-8c40-88f62656a70a" name="ID-e06ce975-464b-495e-8c40-88f62656a70a"></a>Beyond parentheses</h3>
<div class="outline-text-3" id="text-1-4">
<p>
You may ask, can you to create syntax without parentheses in Lisp? Of
course, you can create entirely new syntax to extend Lisp without
being enclosed inside the parentheses of Lisp, using reader
macro. The difference between reader macro and macro:
</p>
<ul class="org-ul">
<li>A reader macro transforms raw text into valid Lisp objects. Reader
macro is a special type of macro that allows you to transform
non-Lisp code into Lisp code.
</li>
<li>A regular macro transforms Lisp's list into valid Lisp code.
</li>
</ul>
<p>
For example, you can remove the parentheses with the Python
<code>if..then..else</code> using a reader macro and use a non-parentheses Python
<code>if..then..else</code> validly in your program. Using a regular macro, you
have to keep the parentheses to make it a valid Lisp object: a list of
symbols, then that list will be transformed at compile time. Lisp
macro is advanced topic, and should really master the basics before
getting to it.
</p>
</div>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2"><a id="ID-f81bda6e-00ea-47ee-8250-308ec8109f47" name="ID-f81bda6e-00ea-47ee-8250-308ec8109f47"></a>Syntax error</h2>
<div class="outline-text-2" id="text-2">
<p>
Lisp syntax is simple: it's just a pair of parentheses, with things in
in it: <code>(...)</code>. If you encounter syntax errors, it belongs to these
two cases:
</p>
</div>
<div id="outline-container-sec-2-1" class="outline-3">
<h3 id="sec-2-1"><a id="ID-64829a4d-b80d-4bac-9941-f45367d7edc3" name="ID-64829a4d-b80d-4bac-9941-f45367d7edc3"></a>Unbalanced parentheses:</h3>
<div class="outline-text-3" id="text-2-1">
<p>
Do you miss an opening or closing parentheses, or do you insert
unnecessary parentheses? Incorrect usage of parentheses is the only
syntax error you get when writing Lisp program. In other languages,
you have to remember many syntax rules. For example, to write a <code>for</code>
in Tcl, you have to write like this to make it valid
</p>
<div class="org-src-container">
<pre class="src src-tcl"><span style="color: #a020f0;">for</span> <span style="color: #707183;">{</span><span style="color: #483d8b;">set</span> <span style="color: #7a4f7e;">i</span> <span style="color: #008b8b;">0</span><span style="color: #707183;">}</span> <span style="color: #707183;">{</span><span style="color: #43783f;">$</span><span style="color: #43783f;">i</span> <span style="color: #43783f;"><</span> <span style="color: #5e7837;">$</span><span style="color: #5e7837;">n</span><span style="color: #707183;">}</span> <span style="color: #707183;">{</span><span style="color: #483d8b;">incr</span> <span style="color: #7a4f7e;">i</span><span style="color: #707183;">}</span> <span style="color: #707183;">{</span>
<span style="color: #783778;">...do</span> <span style="color: #707e4f;">something...</span>
<span style="color: #707183;">}</span>
</pre>
</div>
<p>
I kept forgetting all the times when I first used it because I get
used to C style for loop. In Tcl, to use some variables, you have to
put a dollar sign <code>$</code> before the variable names. Howver, in some
context, you must not insert dollar before:
</p>
<div class="org-src-container">
<pre class="src src-tcl"><span style="color: #483d8b;">array</span> <span style="color: #483d8b;">set</span> <span style="color: #783778;">balloon</span> <span style="color: #707183;">{</span><span style="color: #784437;">color</span> <span style="color: #707e4f;">red</span><span style="color: #707183;">}</span>
<span style="color: #483d8b;">array</span> <span style="color: #5e7837;">get</span> <span style="color: #783778;">balloon</span>
</pre>
</div>
<p>
<code>balloon</code> is an array variable, but to use it you must not insert dollar
sign before. It's annoying to remember trivial details like this.
</p>
</div>
</div>
<div id="outline-container-sec-2-2" class="outline-3">
<h3 id="sec-2-2"><a id="ID-e98cbd6b-60c8-447e-bf1d-e567b4d200c9" name="ID-e98cbd6b-60c8-447e-bf1d-e567b4d200c9"></a>Mini-language syntax error:</h3>
<div class="outline-text-3" id="text-2-2">
<p>
If you create a mini language, then you must follow its syntax
rules. In this case, you get syntax errors like regular languages
if you code is not correct according to syntax rules. However, if you
are a beginner, you won't have to worry about macro and mini-languages
at this stage.
</p>
</div>
</div>
</div>
<div id="outline-container-sec-3" class="outline-2">
<h2 id="sec-3"><a id="ID-a36aca24-f1ba-4371-8e5e-12d545337e78" name="ID-a36aca24-f1ba-4371-8e5e-12d545337e78"></a>Semantic error</h2>
<div class="outline-text-2" id="text-3">
<p>
You might wonder, parentheses cannot be the only source of
errors. What would happen when incorrect number of arguments passed
into a function? Or non-existent variables, incorrect variable types,
array index out of range…? These errors are called <b>semantic
errors</b>. It has nothing to do with how statements are constructed.
</p>
<p>
For example, this is syntax error:
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #483d8b;">#include</span> <span style="color: #8b2252;"><stdio.h></span>
<span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span> <span style="color: #707183;">(</span><span style="color: #228b22;">int</span> <span style="color: #783f5a;">argc</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span>* <span style="color: #3f7178;">argv</span><span style="color: #7388d6;">[]</span><span style="color: #707183;">)</span> <span style="color: #707183;">{</span>
<span style="color: #a020f0;">if</span> <span style="color: #783f5a;">argc</span> == <span style="color: #008b8b;">1</span> <span style="color: #7388d6;">{</span> <span style="color: #374478;">exit</span><span style="color: #909183;">(</span><span style="color: #008b8b;">1</span><span style="color: #909183;">)</span> <span style="color: #7388d6;">}</span>
<span style="color: #3f7178;">printf</span><span style="color: #7388d6;">(</span><span style="color: #8b2252;">"Hello world"</span><span style="color: #7388d6;">)</span>
<span style="color: #707183;">}</span>
</pre>
</div>
<p>
In the above example, I made two syntax errors:
</p>
<ul class="org-ul">
<li>the condition in <code>if</code> statement is not surrounded by a pair of
parentheses. <code>if</code> statement in C requires this generic form:
</li>
</ul>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #a020f0;">if</span> <span style="color: #707183;">(</span><span style="color: #513f78;">expression</span><span style="color: #707183;">)</span> <span style="color: #707183;">{</span>
...<span style="color: #784437;">statements</span> <span style="color: #78683f;">separated</span> <span style="color: #5e7837;">by</span> <span style="color: #4f7e67;">semicolon</span>...
<span style="color: #707183;">}</span>
</pre>
</div>
<ul class="org-ul">
<li>missed a semicolon <code>;</code> at the end of <code>printf</code> statement.
</li>
</ul>
<p>
In contrast, this is semantic error:
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">void</span> <span style="color: #0000ff;">add</span><span style="color: #707183;">(</span><span style="color: #228b22;">int</span> <span style="color: #78683f;">a</span>, <span style="color: #228b22;">int</span> <span style="color: #78683f;">b</span><span style="color: #707183;">)</span> <span style="color: #707183;">{</span>
<span style="color: #a020f0;">return</span> <span style="color: #78683f;">a</span> + <span style="color: #78683f;">b</span>;
<span style="color: #707183;">}</span>
<span style="color: #228b22;">void</span> <span style="color: #0000ff;">main</span><span style="color: #707183;">(</span><span style="color: #228b22;">int</span> <span style="color: #783f5a;">argc</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span>* <span style="color: #3f7178;">argv</span><span style="color: #7388d6;">[]</span><span style="color: #707183;">)</span> <span style="color: #707183;">{</span>
<span style="color: #228b22;">int</span> <span style="color: #78683f;">a</span> = <span style="color: #008b8b;">1</span>;
<span style="color: #4f7e67;">add</span><span style="color: #7388d6;">(</span><span style="color: #78683f;">a</span><span style="color: #7388d6;">)</span>;
<span style="color: #4f7e67;">add</span><span style="color: #7388d6;">(</span><span style="color: #78683f;">a</span>,<span style="color: #78683f;">b</span><span style="color: #7388d6;">)</span>;
<span style="color: #707183;">}</span>
</pre>
</div>
<p>
The calls to <code>add</code> are syntactically correct, but used incorrectly:
the first call to add requires one more argument; the second call to
add contains non-existent variable.
</p>
<p>
As in other languages, Lisp treats these errors as semantic errors,
since syntax errors in Lisp have only to do with parentheses.
</p>
</div>
</div>
<div id="outline-container-sec-4" class="outline-2">
<h2 id="sec-4"><a id="ID-b82bfb73-1af9-48d1-9565-0b8541e27487" name="ID-b82bfb73-1af9-48d1-9565-0b8541e27487"></a>Lisp Machine</h2>
<div class="outline-text-2" id="text-4">
<p>
It would be a mistake when mention about history of Lisp without
mention about the Lisp Machine, a computing system that is built to
run Lisp natively. In a Lisp Machine, the Operating System, device
drivers and applications are written using a single language:
Lisp. Such a thing is possible because the computer has a built-in
hardware garbage collector, as opposed to the software implementations
in garbage collected languages today.
</p>
<p>
<a href="http://www.andromeda.com/people/ddyer/lisp/">A Brief History of Lisp Machines</a>
</p>
<blockquote>
<p>
<b>Why Lisp?</b> Everyone "knows" that lisp was the language of choice for
Artificial Intelligence research, but a big part of AI research is
about paradigms for representing knowledge, expressing algorithms,
man-machine communication, and machine-to-machine communication: In
short, how to use computers in general. Lisp, as the default AI
language, was also an important research vehicle for new computer
languages, networking, display technology and so on.
</p>
<p>
<b>Why Lisp Machines?</b> The standard platform for Lisp before Lisp
machines was a timeshared PDP-10, but it was well known that one Lisp
program could turn a timeshared KL-10 into unusable sludge for
everyone else. It became technically feasible to build cheaper
hardware that would run lisp better than on timeshared computers. The
technological push was definitely from the top down; to run big,
resource hungry lisp programs more cheaply. Lisp machines were not
"personal" out of some desire make life pleasant for programmers, but
simply because lisp would use 100% of whatever resources it had
available. All code on these systems was written in Lisp simply
because that was the easiest and most cost effective way to provide an
operating system on this new hardware.
</p>
<p>
<b>Why two different kinds?</b> Quite a few groups with different goals were
building high priced, high powered workstations at about the same
time. All were capitalizing on Moore's law and the emerging consensus
that bitmapped displays, windows, mice, and networks were effective
paradigms. The C/Unix community spawned Sun, Apollo, and Silicon
Graphics. The Pascal Community spawned the PERQ. There were two
major branches in the Lisp family tree, Interlisp and Maclisp, so it
should be no surprise that there were two main family branches in Lisp
machines.
</p>
<p>
Today, all this hardware and software are commercially extinct, but
many features that were commercialized by lispms are present in every
PC.
</p>
</blockquote>
<p>
<span class="underline">Futher resources</span>:
</p>
<p>
<a href="http://en.wikipedia.org/wiki/Genera_%2528operating_system%2529">Lisp OS: Genera</a>: The OS is written entirely in Lisp, both the
Operating System and the high-level applications.
</p>
<p>
<a href="http://www.sts.tu-harburg.de/~r.f.moeller/symbolics-info/development-environment/index.html">The Lisp Machine Software Development Environment</a>
</p>
<p>
<a href="http://smbx.org/">Symbolic Lisp Machine Museum</a>
</p>
<p>
<a href="http://www.sts.tu-harburg.de/~r.f.moeller/symbolics-info/symbolics.html">Symbolics Lisp Machine Museum provided by Ralf Möller</a>
</p>
<p>
<a href="http://www.loper-os.org/?p=932">Kalman Reti, the Last Symbolics Developer, Speaks of Lisp Machines</a>
</p>
<p>
<a href="http://www.loper-os.org/?p=52">Secrets of the Symbolics Console: Part 1</a>
</p>
<p>
<a href="http://www.loper-os.org/?p=53">Secrets of the Symbolics Console: Part 2</a>
</p>
<p>
<a href="http://fare.tunes.org/LispM.html">A few things I know about LISP Machines</a>
</p>
<p>
<a href="http://dspace.mit.edu/handle/1721.1/5718">MIT's CADR machine</a>
</p>
</div>
</div>
<div id="outline-container-sec-5" class="outline-2">
<h2 id="sec-5"><a id="ID-125d8641-5327-4169-94c5-969e06d4bf69" name="ID-125d8641-5327-4169-94c5-969e06d4bf69"></a>Conclusion</h2>
<div class="outline-text-2" id="text-5">
<p>
You won't find any language with such a minimal syntax and uniformity,
yet so expressive, since you can choose any language syntax that you
want to solve your problems in. Some languages also have <b>homoiconic</b>
property, but instead of using just a pair of parentheses, they use
more complex syntax constructs. Some languages are simple (still not
as much as Lisp), but are not <b>homoiconic</b>. The only syntax you write
in Lisp, again, just a pair of parentheses, with things in
in it: <code>(...)</code>. Because of syntax like this, Lisp requires you to
careful match the parentheses. Or you can let Emacs does it for you.
</p>
<p>
Learning any language has something in common:
</p>
<ul class="org-ul">
<li>Learn syntax and semantic.
</li>
<li>Learn idiomatic ways of using the language.
</li>
<li>Learn commonly used libraries.
</li>
<li>Learn common development tools used with the language.
</li>
</ul>
<p>
We already covered the first. I will show you how to use common
functions for configuration, and setup a programming environment for
any Lisp in the next chapter.
</p>
</div>
</div>
</div>
<div id="postamble" class="status">
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'emacs-tutor'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div>
</body>
</html>