File size: 40,894 Bytes
7a045c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
<html>
<head>
<title>LISP Tutorial 1: Basic LISP Programming</title>
</head>
<body BGCOLOR=FFFFFF>
<h1>LISP Tutorial 1: Basic LISP Programming</h1>



<h2>LISP Expressions</h2>

<p>When you start up the Common LISP environment, you should
see a prompt, which means that LISP is waiting for you to enter
a LISP expression.  In the environment I am using, it looks like
the following:
<pre>
USER(1):
</pre>
The Common LISP environment follows the algorithm below
when interacting with users:
<pre>
loop
    read in an expression from the console;
    evaluate the expression;
    print the result of evaluation to the console;
end loop.
</pre>
Common LISP reads in an expression, evaluates it,
and then prints out the result.  For example, if you want to
compute the value of <em>(2 * cos(0) * (4 + 6))</em>, you type in:
<pre>
USER(1): (* 2 (cos 0) (+ 4 6))
</pre>
Common LISP replies:
<pre>
20.0
</pre>
before prompting you to enter the next expression.  
Several things are worth noting:
<ul>
<li>LISP expressions are composed of <em>forms</em>.
    The most common LISP form is <em>function application</em>.
    LISP represents a function call <em>f(x)</em>
    as <tt>(f x)</tt>.  For example, <em>cos(0)</em>
    is written as <tt>(cos 0)</tt>.  
<li>LISP expressions are case-insensitive.  It makes 
    no difference whether we type <tt>(cos 0)</tt> or
    <tt>(COS 0)</tt>.
<li>Similarly, "<tt>+</tt>" is the name of the
    addition function that returns the sum of
    its arguments.
<li>Some functions, like "<tt>+</tt>" and "<tt>*</tt>",
    could take an arbitrary number of arguments.  In our
    example, "<tt>*</tt>" took three arguments.  It could
    as well take 2 arguments, as in "<tt>(* 2 3)</tt>", or
    4 arguments, as in "<tt>(* 2 3 4 5)</tt>".
<li>In general,
    a function application form looks like 
    <tt>(<em>function</em> 
    <em>argument<sub>1</sub></em> <em>argument<sub>2</sub></em> 
    ... <em>argument<sub>n</sub></em>)</tt>.
    As in many programming languages (e.g. C/C++),
    LISP evaluates function calls in <em>applicative order</em>, which 
    means that all the argument forms are evaluated
    before the function is invoked.  That is to say, the
    argument forms <tt>(cos 0)</tt>
    and <tt>(+ 4 6)</tt> are respectively evaluated to the 
    values <em>1</em> and 
    <em>10</em> before they are passed as arguments to the <tt>*</tt>
    function.
    Some other forms, like
    the <em>conditionals</em> we will see later, are not evaluated in
    applicative order.
<li>Numeric values like <tt>4</tt> and <tt>6</tt> are
    called <em>self-evaluating forms</em>: they evaluate
    to themselves.  To evaluate <tt>(+ 4 6)</tt> in  applicative order, 
    the forms <tt>4</tt> and <tt>6</tt> are respectively evaluated
    to the values <em>4</em> and <em>6</em> before they are passed
    as arguments to the <tt>+</tt> function.
</ul>

<p>Complex arithmatic expressions can be constructed from built-in
functions like the following:
<p><table border=1>
<tr>
<th>Numeric Functions</th><th>Meaning</th>
</tr>
<tr>
<td><tt>(+ <em>x<sub>1</sub></em> <em>x<sub>2</sub></em> ... <em>x<sub>n</sub></em>)</tt></td>
<td>The sum of <em>x<sub>1</sub></em>, <em>x<sub>2</sub></em>, ..., <em>x<sub>n</sub></em></td>
</tr>
<tr>
<td><tt>(* <em>x<sub>1</sub></em> <em>x<sub>2</sub></em> ... <em>x<sub>n</sub></em>)</tt></td>
<td>The product of <em>x<sub>1</sub></em>, <em>x<sub>2</sub></em>, ..., <em>x<sub>n</sub></em></td>
</tr>
<tr>
<td><tt>(- <em>x</em> <em>y</em>)</tt></td>
<td>Subtract <em>y</em> from <em>x</em></td>
</tr>
<tr>
<td><tt>(/ <em>x</em> <em>y</em>)</tt></td>
<td>Divide <em>x</em> by <em>y</em></td>
</tr>
<tr>
<td><tt>(rem <em>x</em> <em>y</em>)</tt></td>
<td>The remainder of dividing <em>x</em> by <em>y</em></td>
</tr>
<tr>
<td><tt>(abs <em>x</em>)</tt></td>
<td>The absolute value of <em>x</em></td>
</tr>
<tr>
<td><tt>(max <em>x<sub>1</sub></em> <em>x<sub>2</sub></em> ... <em>x<sub>n</sub></em>)</tt></td>
<td>The maximum of <em>x<sub>1</sub></em>, <em>x<sub>2</sub></em>, ..., <em>x<sub>n</sub></em></td>
</tr>
<tr>
<td><tt>(min <em>x<sub>1</sub></em> <em>x<sub>2</sub></em> ... <em>x<sub>n</sub></em>)</tt></td>
<td>The minimum of <em>x<sub>1</sub></em>, <em>x<sub>2</sub></em>, ..., <em>x<sub>n</sub></em></td>
</tr>
</table>
<p>Common LISP has a rich set of pre-defined numerical functions.
For a complete coverage, consult Chapter 12 of the book, <em>Common
LISP, The Language (2nd Edition)</em> (CLTL2) by Guy Steele.  In
general, we will not be able to cover all aspects of Common LISP
in this tutorial.
Adventurous readers should consult CLTL2 frequently for more
in-depth explanation of various features of the language.

<p><hr>
<b>Exercise:</b>  Look up pages 376-378 of CLTL2 and find out 
what the functions <tt>floor</tt> and <tt>ceiling</tt> are
for.  Then, find out the subtle difference between <tt>mod</tt>
and <tt>rem</tt>.
<hr>

<h2>Defining Functions</h2>

<p>Evaluating expressions is not very interesting.  We would like to 
build expression abstractions that could be reused in the future.
For example, we could type in the following:
<pre>
USER(2): (defun double (x) (* x 2))
DOUBLE
</pre>
In the above, we define a function named <tt>double</tt>, which
returns two times the value of its input argument <em>x</em>.  We can then
test-drive the function as below:
<pre>
USER(3): (double 3)
6
USER(4): (double 7)
14
</pre>

<h2>Editing, Loading and Compiling LISP Programs</h2>

<p>Most of the functions we would like to write is going to
be a lot longer than the <tt>double</tt> function.
When working with complex programs, it is usually desirable
to edit the program with an editor, fully debug the code,
and then compile it for faster performance.  Use
your favorite text editor (mine is <tt>emacs</tt>) to key in the
following function definition:
<pre>
;;; testing.lisp
;;; by Philip Fong
;;;
;;; Introductory comments are preceded by ";;;"
;;; Function headers are preceded by ";;"
;;; Inline comments are introduced by ";"
;;;

;;
;; Triple the value of a number
;;

(defun triple (X)
  "Compute three times X."  ; Inline comments can
  (* 3 X))                  ; be placed here.

;;
;; Negate the sign of a number
;;

(defun negate (X)
  "Negate the value of X."  ; This is a documentation string.
  (- X))                
</pre>
Save the above in the file <tt>testing.lisp</tt>.  Now load
the definition into the LISP environment by typing:
<pre>
USER(5): (load "testing.lisp")
; Loading ./testing.lisp
T
</pre>
Let us try to see if they are working properly.
<pre>
USER(6): (triple 2)
6
USER(7): (negate 3)
-3
</pre>
When functions are fully debugged, we can also 
compile them into binaries:
<pre>
USER(8): (compile-file "testing.lisp")
</pre>
Depending on whether your code is well-formed, and what system
you are using, some compilation messages will be generated.
The compiled code can be loaded into the LISP environment later by
using the following:
<pre>
USER(9): (load "testing")
; Fast loading ./testing.fasl
T
</pre>

<h2>Control Stuctures: Recursions and Conditionals</h2>

<p>Now that we are equipped with all the tools for 
developing LISP programs, let us venture into
something more interesting.  Consider the definition of 
factorials:
<table>
<tr>
<td>   </td><td><em>n! = 1</em></td><td>  </td><td>if <em>n = 1</em></td>
</tr>
<tr>
<td>   </td><td><em>n! = n * (n - 1)!</em></td><td>   </td><td>if <em>n > 1</em></td>
</tr>
</table>
We can implement a function to compute factorials using
recursion:
<pre>
(defun factorial (N)
  "Compute the factorial of N."
  (if (= N 1)
      1
    (* N (factorial (- N 1)))))
</pre>
The <tt>if</tt> form checks if <tt>N</tt> is one, and
returns one if that is the case, or else returns 
<em>N * (N - 1)!</em>.

Several points are worth noting:
<ul>
<li>The condition expression <tt>(= N 1)</tt> is
    a relational expression.  It returns boolean
    values <tt>T</tt> or <tt>NIL</tt>.  In fact, LISP
    treats <tt>NIL</tt> as false, and everything else
    as true.  Other relational operators include the
    following:
<p><table border=1>
<tr>
<th>Relational Operators</th><th>Meaning</th>
</tr>
<tr>
<td><tt>(= <em>x</em> <em>y</em>)</tt></td>
<td><em>x</em> is equal to <em>y</em></td>
</tr>
<tr>
<td><tt>(/= <em>x</em> <em>y</em>)</tt></td>
<td><em>x</em> is not equal to <em>y</em></td>
</tr>
<tr>
<td><tt>(< <em>x</em> <em>y</em>)</tt></td>
<td><em>x</em> is less than <em>y</em></td>
</tr>
<tr>
<td><tt>(> <em>x</em> <em>y</em>)</tt></td>
<td><em>x</em> is greater than <em>y</em></td>
</tr>
<tr>
<td><tt>(<= <em>x</em> <em>y</em>)</tt></td>
<td><em>x</em> is no greater than <em>y</em></td>
</tr>
<tr>
<td><tt>(>= <em>x</em> <em>y</em>)</tt></td>
<td><em>x</em> is no less than <em>y</em></td>
</tr>
</table>
<p>
<li>The <tt>if</tt> form is not a <em>strict function</em>
    (strict functions evaluate their arguments in applicative
     order).  
    Instead, the <tt>if</tt> form evaluates
    the condition <tt>(= N 1)</tt> before further
    evaluating the other two arguments.
    If the condition evaluates to true, then only the
    second argument is evaluated, and its value is
    returned as the value of the <tt>if</tt> form.
    Otherwise, the third argument is evaluated, and its
    value is returned.  Forms that are not strict 
    functions are called
    <em>special forms</em>.
<li>The function is recursive.  The definition of <tt>factorial</tt>
    involves invocation of itself.  Recursion is, for now, our only
    mechanism for producing looping behavior.  Specifically, 
    the kind of recursion we are looking at is called <em>linear
    recursion</em>, in which the function may make at most one
    recursive call from any level of invocation.
</ul>

<p>To better understand the last point,
we can make use of the debugging facility
<tt>trace</tt> (do not compile your code if you want to use 
<tt>trace</tt>):
<pre>
USER(11): (trace factorial)
(FACTORIAL)
USER(12): (factorial 4)
 0: (FACTORIAL 4)
   1: (FACTORIAL 3)
     2: (FACTORIAL 2)
       3: (FACTORIAL 1)
       3: returned 1
     2: returned 2
   1: returned 6
 0: returned 24
24
</pre>
Tracing <tt>factorial</tt> allows us to examine 
the recursive invocation of the function.  As you
can see, at most one recursive call is made from
each level of invocation.

<p><hr><b>Exercise:</b>
The <em>N</em>'th <em>triangular number</em> is
defined to be <em>1 + 2 + 3 + ... + N</em>.
Alternatively, we could give a recursive definition
of triangular number as follows:
<table>
<tr>
<td></td>
<td><em>T(n) = 1</em></td>
<td></td>
<td>if <em>n = 1</em></td>
</tr>
<tr>
<td></td>
<td><em>T(n) = n + T(n-1)</em></td>
<td></td>
<td>if <em>n > 1</em></td>
</tr>
</table>
Use the recursive definition to
help you implement
 a linearly recursive function <tt>(triangular <em>N</em>)</tt>
that returns the <em>N</em>'th triangular number.
Enter your function definition into a text file.  Then load
it into LISP.  Trace the execution of <tt>(triangular 6)</tt>.
<hr>

<p><hr><b>Exercise:</b>
Write down a recursive definition of <em>B<sup>E</sup></em>
(assuming that
both <em>B</em> and <em>E</em> are non-negative integers).
Then implement a linearly
recursive function <tt>(power <em>B</em> <em>E</em>)</tt> that
computes <em>B<sup>E</sup></em>.
Enter your function definition into a text file.  Then load
it into LISP.  Trace the execution of <tt>(power 2 6)</tt>.
<hr>

<h2>Multiple Recursions</h2>

<p>Recall the definition of Fibonacci numbers:
<table>
<tr>
<td> </td> <td><em>Fib(n) = 1</em></td> <td> </td>
<td>for <em>n = 0</em> or <em>n = 1</em></td>
</tr>
<tr>
<td> </td> <td><em>Fib(n) = Fib(n-1) + Fib(n-2)</em></td> <td> </td>
<td>for <em>n > 1</em></td>
</tr>
</table>
This definition can be directly translated to the
following LISP code:
<pre>
(defun fibonacci (N)
  "Compute the N'th Fibonacci number."
  (if (or (zerop N) (= N 1))
      1
    (+ (fibonacci (- N 1)) (fibonacci (- N 2)))))
</pre>

<p>Again, several observations can be made.
First, the function call <tt>(zerop N)</tt> tests if <tt>N</tt> is
zero.  It is merely a shorthand for <tt>(= N 0)</tt>.  As such,
<tt>zerop</tt> returns either <tt>T</tt> or <tt>NIL</tt>.  We
call such a boolean function a <em>predicate</em>, as indicated by the
suffix <tt>p</tt>.  Some other built-in shorthands and predicates
are the following:
<p><table border=1>
<tr>
<th>Shorthand</th><th>Meaning</th>
</tr>
<tr>
<td><tt>(1+ <em>x</em>)</tt></td>
<td><tt>(+ <em>x</em> 1)</tt></td>
</tr>
<tr>
<td><tt>(1- <em>x</em>)</tt></td>
<td><tt>(- <em>x</em> 1)</tt></td>
</tr>
<tr>
<td><tt>(zerop <em>x</em>)</tt></td><td><tt>(= <em>x</em> 0)</tt></td>
</tr>
<tr>
<td><tt>(plusp <em>x</em>)</tt></td><td><tt>(> <em>x</em> 0)</tt></td>
</tr>
<tr>
<td><tt>(minusp <em>x</em>)</tt></td><td><tt>(< <em>x</em> 0)</tt></td>
</tr>
<tr>
<td><tt>(evenp <em>x</em>)</tt></td><td><tt>(= (rem <em>x</em> 2) 0)</tt></td>
</tr>
<tr>
<td><tt>(oddp <em>x</em>)</tt></td><td><tt>(/= (rem <em>x</em> 2) 0)</tt></td>
</tr>
</table>

<p>Second, the <tt>or</tt> form is a logical operator.  Like <tt>if</tt>,
<tt>or</tt> is not a strict function.  
It evaluates its arguments from left
to right, returning non-<tt>NIL</tt> immediately if it encounters
an argument that evaluates to non-<tt>NIL</tt>.  It evaluates
to <tt>NIL</tt> if all tests fail.
For example, in the expression <tt>(or t (= 1 1))</tt>, the second
argument <tt>(= 1 1)</tt> will not be evaluated.  Similar logical
connectives are listed below:
<p><table border=1>
<tr>
<th>Logical Operators</th><th>Meaning</th>
</tr>
<tr>
<td><tt>(or <em>x<sub>1</sub></em> <em>x<sub>2</sub></em> ... <em>x<sub>n</sub></em>)</tt></td>
<td>Logical or</td>
</tr>
<tr>
<td><tt>(and <em>x<sub>1</sub></em> <em>x<sub>2</sub></em> ... <em>x<sub>n</sub></em>)</tt></td>
<td>Logical and</td>
</tr>
<tr>
<td><tt>(not <em>x</em>)</tt></td>
<td>Logical negation</td>
</tr>
</table>

<p>Third, the function definition contains two self references.
It first recursively evaluates <tt>(fibonacci (- N 1))</tt> to
compute <em>Fib(N-1)</em>, then evaluates <tt>(fibonacci (- N 2))</tt>
to obtain <em>Fib(N-2)</em>, and lastly return their sum.
This kind of recursive definitiion is called <em>double recursion</em>
(more generally, <em>multiple recursion</em>).
Tracing the function yields the following:
<pre>
USER(20): (fibonacci 3)
 0: (FIBONACCI 3)
   1: (FIBONACCI 2)
     2: (FIBONACCI 1)
     2: returned 1
     2: (FIBONACCI 0)
     2: returned 1
   1: returned 2
   1: (FIBONACCI 1)
   1: returned 1
 0: returned 3
3
</pre>
Note that in each level, there could be up to two recursive invocations.


<p><hr><b>Exercise</b>: The Binomial Coefficient <em>B(n, r)</em> is
the coefficient of the term <em>x<sup>r</sup></em> in the binormial
expansion of 
<em>(1 + x)<sup>n</sup></em>.
For example, <em>B(4, 2) = 6</em> because <em>(1+x)<sup>4</sup>
= 1 + 4x + 6x<sup>2</sup> + 4x<sup>3</sup> + x<sup>4</sup></em>.
The Binomial Coefficient
 can be computed using the <em>Pascal Triangle</em> formula:
<table>
<tr>
<td> </td> <td><em>B(n, r) = 1</em></td> <td> </td>
<td>if <em>r = 0</em> or <em>r = n</em></td>
</tr>
<tr>
<td> </td><td><em>B(n, r) = B(n-1, r-1) + B(n-1, r)</td> <td> </td>
<td>otherwise</td>
</tr>
</table>
Implement a doubly recursive function <tt>(binomial <em>N</em> <em>R</em>)</tt>
that computes the binomial coefficient <em>B(N, R)</em>.
<hr>

<p>Some beginners might find nested function calls like the
following very difficult to understand:
<pre>
    (+ (fibonacci (- N 1)) (fibonacci (- N 2)))))
</pre>
To make such expressions easier to write and comprehend, one
could define local name bindings to represent intermediate
results:
<pre>
    (let
	((F1 (fibonacci (- N 1)))
	 (F2 (fibonacci (- N 2))))
      (+ F1 F2))
</pre>
The <tt>let</tt> special form above defines two local variables,
<tt>F1</tt> and <tt>F2</tt>, which binds to <em>Fib(N-1)</em>
and <em>Fib(N-2)</em> respectively.  Under these local
bindings, <tt>let</tt> evaluates <tt>(+ F1 F2)</tt>.
The <tt>fibonacci</tt> function can thus be rewritten as follows:
<pre>
(defun fibonacci (N)
  "Compute the N'th Fibonacci number."
  (if (or (zerop N) (= N 1))
      1
    (let
	((F1 (fibonacci (- N 1)))
	 (F2 (fibonacci (- N 2))))
      (+ F1 F2))))
</pre>

<p>Notice that <tt>let</tt> creates all bindings in parallel.
That is, both <tt>(fibonacci (- N 1))</tt> and <tt>(fibonacci (- N 2))</tt>
are evaluated first, and then they are bound to <tt>F1</tt> and <tt>F2</tt>.
This means that the following LISP code will not work:
<pre>
(let
    ((x 1)
     (y (* x 2)))
  (+ x y))
</pre>
LISP will attempt to evaluate the right hand sides first before 
the bindings are established.  So, the expression
<tt>(* x 2)</tt> is evaluated before the binding of <tt>x</tt>
is available.  To perform sequential binding, use the <tt>let*</tt>
form instead:
<pre>
(let*
    ((x 1)
     (y (* x 2)))
  (+ x y))
</pre>
LISP will bind <tt>1</tt> to <tt>x</tt>, then evaluate <tt>(* x 2)</tt>
before the value is bound to <tt>y</tt>.

<h2>Lists</h2>

<p>Numeric values are not the only type of data LISP supports.
LISP is designed for symbolic computing.  The fundamental
LISP data structure for supporting symbolic manipulation are
lists.  In fact, LISP stands for "LISt Processing."  

<p>Lists are containers that supports sequential traversal.
List is also a <em>recursive data structure</em>: its definition
is recursive.
As such, most of its traversal algorithms are recursive functions.
In order to better
understand a recursive abstract data type and prepare oneself
to develop recursive operations on the data type, one should present
the data type in terms of its <em>constructors</em>,
<em>selectors</em> and <em>recognizers</em>.

<p>Constructors are forms that create new
instances of a data type (possibly out of some simpler components).  
A list is obtained by
evaluating one of the following constructors:
<ol>
<li><tt>nil</tt>: Evaluating <tt>nil</tt> creates an <em>empty</em>
     list;
<li><tt>(cons <em>x</em> <em>L</em>)</tt>: Given a LISP object
 <em>x</em> and a list <em>L</em>, evaluating <tt>(cons <em>x</em> <em>L</em>)</tt>
  creates a list 
  containing 
<em>x</em> followed by the elements in <em>L</em>.  
</ol>
<p>Notice that the above definition is inherently recursive.
For example,
to construct a list containing 1 followed by 2, we could type
in the expression:
<pre>
USER(21): (cons 1 (cons 2 nil))
(1 2)
</pre>
LISP replies by printing <tt>(1 2)</tt>, which is a more
readable representation of a list containing 1 followed by 2.
To understand why the above works, notice that <tt>nil</tt>
is a list (an empty one), and thus <tt>(cons 2 nil)</tt> is
also a list (a list containing 1 followed by nothing).  Applying
the second constructor again, we see that <tt>(cons 1 (cons 2 nil))</tt>
is also a list (a list containing 1 followed by 2 followed by nothing).


<p>Typing <tt>cons</tt> expressions could be tedious.
If we already know all the elements in a list, we could 
enter our list as <em>list literals</em>.  For example, to 
enter a list containing all prime numbers less than 20,
we could type in the following expression:
<pre>
USER(22): (quote (2 3 5 7 11 13 17 19))
(2 3 5 7 11 13 17 19)
</pre>
Notice that we have quoted the list using the <tt>quote</tt> special
form.  This is necessary
because, without the quote, LISP would interpret the expression
<tt>(2 3 5 7 11 13 17 19)</tt>
as a function call to a function with name "2" and arguments 3, 5, ..., 19
The <tt>quote</tt> is just a syntactic device that instructs LISP 
not to evaluate the a form in applicative order, but rather treat it
as a literal.
Since quoting is used frequently in LISP programs,
there is a shorthand for <tt>quote</tt>:
<pre>
USER(23): '(2 3 5 7 11 13 17 19)
(2 3 5 7 11 13 17 19)
</pre>
The quote symbol <tt>'</tt> is nothing but a syntactic shorthand for
<tt>(quote ...)</tt>.

<p>The second ingredient of an abstract data type are its selectors.
Given a composite object constructed out of several components,
a selector form returns one of its components.  Specifically,
suppose a list <em>L<sub>1</sub></em> is constructed by evaluating
<tt>(cons <em>x</em> <em>L<sub>2</sub></em>)</tt>, where <em>x</em> is a
LISP object and <em>L<sub>2</sub></em> is a list.  Then, the selector forms
<tt>(first <em>L<sub>1</sub></em>)</tt> and <tt>(rest <em>L<sub>1</sub></em>)</tt> evaluate
to <em>x</em> and <em>L<sub>2</sub></em> respectively, as the following 
examples illustrate:
<pre>
USER(24): (first '(2 4 8))
2
USER(25): (rest '(2 4 8))
(4 8)
USER(26): (first (rest '(2 4 8)))
4
USER(27): (rest (rest '(2 4 8)))
(8)
USER(28): (rest (rest (rest '(8))))
NIL
</pre>

<p>Finally, we look at recognizers, expressions that test how an
object is constructed.  Corresponding to each constructor of
a data type is a recognizer.  In the case of list, they are 
<tt>null</tt> for <tt>nil</tt> and <tt>consp</tt> for <tt>cons</tt>.
Given a list <em>L</em>, <tt>(null <em>L</em>)</tt> returns 
<tt>t</tt> iff <em>L</em> is <tt>nil</tt>, and <tt>(consp <em>L</em>)</tt>
returns <tt>t</tt> iff <em>L</em> is constructed from <tt>cons</tt>.
<pre>
USER(29): (null nil)
T
USER(30): (null '(1 2 3))
NIL
USER(31): (consp nil)
NIL
USER(32): (consp '(1 2 3))
T
</pre>
Notice that, since lists have only two constructors, the recognizers
are complementary.  Therefore, we usually need only one of them.
In our following discussion, we use only <tt>null</tt>.

<h2>Structural Recursion with Lists</h2>

<p>As we have promised, understanding how the constructors, selectors and
recognizers of lists work helps us to develop recursive functions that
traverse a list.  Let us begin with an example.
The LISP built-in function <tt>list-length</tt> counts the number of
elements in a list.  For example,
<pre>
USER(33): (list-length '(2 3 5 7 11 13 17 19))
8
</pre>
Let us try to see how such a function can be implemented recursively.
A given list <em>L</em> is created by either one of the two constructors, 
namely <tt>nil</tt> or a <tt>cons</tt>:
<ul>
<li><em>Case 1</em>: <em>L</em> is <tt>nil</tt>.<br>
  The length of an empty list is zero.
<li><em>Case 2</em>: <em>L</em> is constructed by <tt>cons</tt>.<br>
  Then <em>L</em> is composed of two parts, namely,
<tt>(first <em>L</em>)</tt> and <tt>(rest <em>L</em>)</tt>.  In such
case, the length of <em>L</em> can be obtained inductively by adding
1 to the length of <tt>(rest <em>L</em>)</tt>. 
</ul>
<p>Formally, we could
implement our own version of <tt>list-length</tt> as follows:
<pre>
(defun recursive-list-length (L)
  "A recursive implementation of list-length."
  (if (null L)
      0
    (1+ (recursive-list-length (rest L)))))
</pre>
Here, we use the recognizer <tt>null</tt> to differentiate how
<em>L</em> is constructed.  In case <em>L</em> is <tt>nil</tt>,
we return 0 as its length.  Otherwise, <em>L</em> is a <tt>cons</tt>,
and we return 1 plus the length of <tt>(rest <em>L</em>)</tt>.
Recall that <tt>(1+ <em>n</em>)</tt> is simply a shorthand for
<tt>(+ <em>n</em> 1)</tt>.

<p>Again, it is instructive to use the trace facilities to
examine the unfolding of recursive invocations:
<pre>
USER(40): (trace recursive-list-length)
(RECURSIVE-LIST-LENGTH)
USER(41): (recursive-list-length '(2 3 5 7 11 13 17 19))
 0: (RECURSIVE-LIST-LENGTH (2 3 5 7 11 13 17 19))
   1: (RECURSIVE-LIST-LENGTH (3 5 7 11 13 17 19))
     2: (RECURSIVE-LIST-LENGTH (5 7 11 13 17 19))
       3: (RECURSIVE-LIST-LENGTH (7 11 13 17 19))
         4: (RECURSIVE-LIST-LENGTH (11 13 17 19))
           5: (RECURSIVE-LIST-LENGTH (13 17 19))
             6: (RECURSIVE-LIST-LENGTH (17 19))
               7: (RECURSIVE-LIST-LENGTH (19))
                 8: (RECURSIVE-LIST-LENGTH NIL)
                 8: returned 0
               7: returned 1
             6: returned 2
           5: returned 3
         4: returned 4
       3: returned 5
     2: returned 6
   1: returned 7
 0: returned 8
8
</pre>

<p>The kind of recursion we see here is called <em>structural
recursion</em>.  Its standard pattern  is as follows.  To
process an instance <em>X</em> of a recursive data type:
<ol>
<li>Use the recognizers to determine how <em>X</em> is created
    (i.e. which constructor creates it).  In our example, we
    use <tt>null</tt> to decide if a list is created by
    <tt>nil</tt> or <tt>cons</tt>.
<li>For instances that are atomic (i.e. those created by 
    constructors with no components), return a trivial
    value.  For example, in the case when a list is <tt>nil</tt>,
    we return zero as its length.
<li>If the instance is composite, then use the selectors to
    extract its components.  In our example, we use <tt>first</tt>
    and <tt>rest</tt> to extract the two components of a nonempty
    list.
<li>Following that, we apply recursion on one or more components
    of <em>X</em>.  For instance, we recusively invoked 
    <tt>recursive-list-length</tt> on <tt>(rest L)</tt>.
<li>Finally, we use either the constructors or some other
    functions to combine the result of the recursive calls,
    yielding the value of the function.  In the case of 
    <tt>recursive-list-length</tt>, we return one plus the
    result of the recursive call.
</ol>

<p><hr><b>Exercise</b>: Implement a linearly recursive 
function <tt>(sum <em>L</em>)</tt> which
computes the sum of all numbers in a list <em>L</em>.
Compare your solution with the standard pattern of
structural recursion.
<hr>

<p>Sometimes, long traces like the one for list-length 
may be difficult to read on a terminal screen.
Common LISP allows you to capture screen I/O into a file
so that you can, for example, produce a hard copy for
more comfortable reading.  To capture the trace of
executing <tt>(recursive-list-length '(2 3 5 7 11 13 17 19))</tt>,
we use the <tt>dribble</tt> command:
<pre>
USER(42): (dribble "output.txt")
dribbling to file "output.txt"
 
NIL
USER(43): (recursive-list-length '(2 3 5 7 11 13 17 19))
 0: (RECURSIVE-LIST-LENGTH (2 3 5 7 11 13 17 19))
   1: (RECURSIVE-LIST-LENGTH (3 5 7 11 13 17 19))
     2: (RECURSIVE-LIST-LENGTH (5 7 11 13 17 19))
       3: (RECURSIVE-LIST-LENGTH (7 11 13 17 19))
         4: (RECURSIVE-LIST-LENGTH (11 13 17 19))
           5: (RECURSIVE-LIST-LENGTH (13 17 19))
             6: (RECURSIVE-LIST-LENGTH (17 19))
               7: (RECURSIVE-LIST-LENGTH (19))
                 8: (RECURSIVE-LIST-LENGTH NIL)
                 8: returned 0
               7: returned 1
             6: returned 2
           5: returned 3
         4: returned 4
       3: returned 5
     2: returned 6
   1: returned 7
 0: returned 8
8
USER(44): (dribble)
</pre>
The form <tt>(dribble "output.txt")</tt> instructs Common LISP
to begin capturing all terminal I/O into a file called <tt>output.txt</tt>.
The trailing <tt>(dribble)</tt> form instructs Common LISP to 
stop I/O capturing, and closes the file <tt>output.txt</tt>.  If we
examine <tt>output.txt</tt>, we will see the following:
<pre>
dribbling to file "output.txt"
 
NIL
USER(43): (recursive-list-length '(2 3 5 7 11 13 17 19))
 0: (RECURSIVE-LIST-LENGTH (2 3 5 7 11 13 17 19))
   1: (RECURSIVE-LIST-LENGTH (3 5 7 11 13 17 19))
     2: (RECURSIVE-LIST-LENGTH (5 7 11 13 17 19))
       3: (RECURSIVE-LIST-LENGTH (7 11 13 17 19))
         4: (RECURSIVE-LIST-LENGTH (11 13 17 19))
           5: (RECURSIVE-LIST-LENGTH (13 17 19))
             6: (RECURSIVE-LIST-LENGTH (17 19))
               7: (RECURSIVE-LIST-LENGTH (19))
                 8: (RECURSIVE-LIST-LENGTH NIL)
                 8: returned 0
               7: returned 1
             6: returned 2
           5: returned 3
         4: returned 4
       3: returned 5
     2: returned 6
   1: returned 7
 0: returned 8
8
USER(44): (dribble)
</pre>

<h2>Symbols</h2>


<p>The lists we have seen so far are lists of numbers.  Another data
type of LISP is <em>symbols</em>.  A symbol is simply a sequence
of characters:
<pre>
USER(45): 'a           ; LISP is case-insensitive.
A
USER(46): 'A           ; 'a and 'A evaluate to the same symbol.
A
USER(47): 'apple2      ; Both alphanumeric characters ...
APPLE2
USER(48): 'an-apple    ; ... and symbolic characters are allowed.
AN-APPLE
USER(49): t            ; Our familiar t is also a symbol.
T
USER(50): 't           ; In addition, quoting is redundant for t.
T
USER(51): nil          ; Our familiar nil is also a symbol.
NIL
USER(52): 'nil         ; Again, it is self-evaluating.
NIL
</pre>

<p>With symbols, we can build more interesting lists:
<pre>
USER(53): '(how are you today ?)   ; A list of symbols.
(HOW ARE YOU TODAY ?)
USER(54): '(1 + 2 * x)             ; A list of symbols and numbers.
(1 + 2 * X)
USER(55): '(pair (2 3))            ; A list containing 'pair and '(2 3).
(pair (2 3))
</pre>
Notice that the list <tt>(pair (2 3))</tt>
has length 2:
<pre>
USER(56): (recursive-list-length '(pair (2 3)))
2
</pre>
Notice also the result of applying accessors:
<pre>
USER(57): (first '(pair (2 3)))
PAIR
USER(58): (rest '(pair (2 3)))
((2 3))
</pre>
Lists containing other lists as members are difficult to understand
for beginners.  Make sure you understand the above example.

<h2>Example: <tt>nth</tt></h2>

<p>LISP defines a function <tt>(nth <em>N</em> <em>L</em>)</tt>
that returns the <em>N</em>'th member of list <em>L</em> (assuming
that the elements are numbered from zero onwards):
<pre>
USER(59): (nth 0 '(a b c d))
A
USER(60): (nth 2 '(a b c d))
C
</pre>
We could implement our own version of <tt>nth</tt> by linear recursion.
Given <em>N</em> and <em>L</em>, either <em>L</em> is <tt>nil</tt>
or it is constructed by <tt>cons</tt>.  
<ul>
<li><em>Case 1</em>: <em>L</em> is <tt>nil</tt>.<br>
Accessing the <em>N</em>'th element is an undefined operation,
and our implementation should arbitrarily return <tt>nil</tt> to 
indicate this.
<li><em>Case 2</em>: <em>L</em> is constructed by a <tt>cons</tt>.<br>
Then <em>L</em>
has two components: <tt>(first <em>L</em>)</tt> and
<tt>(rest <em>L</em>)</tt>.  There are
two subcases: either <em>N = 0</em> or <em>N > 0</em>:
 <ul>
 <li> <em>Case 2.1</em>: <em>N = 0</em>.<br>
      The zeroth element of <em>L</em> is simply
      <tt>(first <em>L</em>)</tt>.
 <li> <em>Case 2.2</em>: <em>N > 0</em>.<br>
The <em>N</em>'th
member of <em>L</em> is exactly the <em>(N-1)</em>'th member of
<tt>(rest <em>L</em>)</tt>.
 </ul>
</ul>
The following code implements our algorithm:
<pre>
(defun list-nth (N L)
  "Return the N'th member of a list L."
  (if (null L)
      nil
    (if (zerop N) 
	(first L)
      (list-nth (1- N) (rest L)))))
</pre>
Recall that <tt>(1- N)</tt> is merely a shorthand for <tt>(- N 1)</tt>.
Notice that both our implementation and its
correctness argument closely follow the standard pattern
of structural recursion.
Tracing the execution of the function, we get:
<pre>
USER(61): (list-nth 2 '(a b c d))
 0: (LIST-NTH 2 (A B C D))
   1: (LIST-NTH 1 (B C D))
     2: (LIST-NTH 0 (C D))
     2: returned C
   1: returned C
 0: returned C
C
</pre>


<p><hr><b>Exercise</b>: LISP has a built-in function <tt>(last <em>L</em>)</tt>
that returns a the last <tt>cons</tt> structure in a given list <em>L</em>.
<pre>
USER(62): (last '(a b c d))
(d)
USER(63): (last '(1 2 3))
(3)
</pre>
Implement your own version of <tt>last</tt>
using linear recursion.  You may assume that <tt>(last nil)</tt>
returns <tt>nil</tt>.  Compare your implementation with
the standard pattern of structural recursion.
<hr>

<p>Notice that we have a standard if-then-else-if structure
in our implementation of <tt>list-nth</tt>.  
Such logic can alternatively
be implemented using the <tt>cond</tt> special form.
<pre>
(defun list-nth (n L)
  "Return the n'th member of a list L."
  (cond
   ((null L)   nil)
   ((zerop n)  (first L))
   (t          (list-nth (1- n) (rest L)))))
</pre>
The <tt>cond</tt> form above is evaluated as follows.  The condition
<tt>(null L)</tt> is evaluated first.  If the result is true,
then <tt>nil</tt> is returned.  Otherwise, the condition <tt>(zerop n)</tt>
is evaluated.  If the condition holds, then the value of <tt>(first L)</tt> is
returned.  In case neither of the conditions holds, 
the value of <tt>(list-nth
(1- n) (rest L))</tt> is returned.

<p><hr><b>Exercise</b>: Survey CLTL2 section 7.6 (pages 156-161) and find
out what other conditional special forms are available in Common LISP.
Do you know when the special forms <tt>when</tt> and <tt>unless</tt>
should be used instead of <tt>if</tt>?
<hr>

<h2>Example: <tt>member</tt></h2>

<p>LISP defines a function <tt>(member <em>E</em> <em>L</em>)</tt>
that returns non-NIL if <em>E</em> is a member of <em>L</em>.
<pre>
USER(64): (member 'b '(perhaps today is a good day to die)) ; test fails
NIL
USER(65): (member 'a '(perhaps today is a good day to die)) ; returns non-NIL
'(a good day to die)
</pre>
We implement our own recursive version as follows:
<pre>
(defun list-member (E L)
  "Test if E is a member of L."
  (cond
   ((null L)          nil)   
   ((eq E (first L))  t)     
   (t                 (list-member E (rest L))))) 
</pre>
The correctness of the above implementation is easy to justify.  The list
<em>L</em> is either constructed by <em>nil</em> or by a call to
<em>cons</em>:
<ul>
<li><em>Case 1</em>: <em>L</em> is <tt>nil</tt>.<br>
<em>L</em> is  
empty, and there is no way <em>E</em> is in <em>L</em>.  
<li><em>Case 2</em>: <em>L</em>
is constructed by <tt>cons</tt><br>
Then it has two components:
<tt>(first <em>L</em>)</tt> and <tt>(rest <em>L</em>)</tt>.
There are two cases, either <tt>(first <em>L</em>)</tt> is <em>E</em>
itself, or it is not.  
<ul>
<li><em>Case 2.1</em>: <em>E</em> equals <tt>(first <em>L</em>)</tt>.<br>
This means that <em>E</em> is a member of <em>L</em>, 
<li><em>Case 2.2</em>: <em>E</em> does not equal <tt>(first <em>L</em>)</tt>.<br>
Then <em>E</em> is a member of <em>L</em> iff <em>E</em> is a 
member of <tt>(rest <em>L</em>)</tt>.
</ul>
</ul>

<p>Tracing the execution of <tt>list-member</tt>, we get the following:
<pre>
USER(70): (list-member 'a '(perhaps today is a good day to die))
 0: (LIST-MEMBER A (PERHAPS TODAY IS A GOOD DAY TO DIE))
   1: (LIST-MEMBER A (TODAY IS A GOOD DAY TO DIE))
     2: (LIST-MEMBER A (IS A GOOD DAY TO DIE))
       3: (LIST-MEMBER A (A GOOD DAY TO DIE))
       3: returned T
     2: returned T
   1: returned T
 0: returned T
T
</pre>

<p>In the implementation of <tt>list-member</tt>,
the function call <tt>(eq <em>x</em> <em>y</em>)</tt> tests if two symbols
are the same.  In fact, the semantics of this test determines what
we mean by a member:
<pre>
USER(71): (list-member '(a b) '((a a) (a b) (a c)))
 0: (LIST-MEMBER (A B) ((A A) (A B) (A C)))
   1: (LIST-MEMBER (A B) ((A B) (A C)))
     2: (LIST-MEMBER (A B) ((A C)))
       3: (LIST-MEMBER (A B) NIL)
       3: returned NIL
     2: returned NIL
   1: returned NIL
 0: returned NIL
NIL
</pre>
In the example above, we would have expected a result of <tt>t</tt>.
However, since <tt>'(a b)</tt> does not <tt>eq</tt> another copy
of <tt>'(a b)</tt> (they are not the same symbol), 
<tt>list-member</tt> returns <tt>nil</tt>.
If we want to account for list equivalence, we could have used
the LISP built-in function <tt>equal</tt> instead of <tt>eq</tt>.
Common LISP defines the following set of predicates for testing
equality:
<p><table border=1>
<tr>
<td><tt>(= <em>x</em> <em>y</em>)</tt></td>
<td>True if <em>x</em> and <em>y</em> evaluate to the same number.</td>
</tr>
<tr>
<td><tt>(eq <em>x</em> <em>y</em>)</tt></td>
<td>True if <em>x</em> and <em>y</em> evaluate to the same symbol.</td>
</tr>
<tr>
<td><tt>(eql <em>x</em> <em>y</em>)</tt></td>
<td>True if <em>x</em> and <em>y</em> are either <tt>=</tt> or <tt>eq</tt>.</td>
</tr>
<tr>
<td><tt>(equal <em>x</em> <em>y</em>)</tt></td>
<td>True if <em>x</em> and <em>y</em> are <tt>eql</tt> or if
    they evaluate to the same list.</td>
</tr>
<tr>
<td><tt>(equalp <em>x</em> <em>y</em>)</tt></td>
<td>To be discussed in Tutorial 4.</td>
</tr>
</table>

<p><hr><b>Exercise</b>:  What would be the behavior of <tt>list-member</tt>
if we replace <tt>eq</tt> by <tt>=</tt>?  By <tt>eql</tt>?  By <tt>equal</tt>?
<hr>

<h2>Example: <tt>append</tt></h2>

<p>LISP defines a function <tt>append</tt> that appends one list by
another:
<pre>
USER(72): (append '(a b c) '(c d e))
(A B C C D E)
</pre>
We implement a recursive version of <tt>append</tt>.
Suppose we are given two lists <em>L1</em> and <em>L2</em>.
<em>L1</em> is either <tt>nil</tt> or constructed by <tt>cons</tt>.
<ul>
<li><em>Case 1</em>:  <em>L1</em> is <tt>nil</tt>.<br>
Appending <em>L2</em> to
<em>L1</em> simply results in <em>L2</em>.  
<li><em>Case 2</em>: <em>L1</em>
is composed of two parts: <tt>(first <em>L1</em>)</tt> and
<tt>(rest <em>L1</em>)</tt>.  If we know the result of appending <em>L2</em>
to <tt>(rest <em>L1</em>)</tt>, then we can take this result, insert
<tt>(first <em>L1</em>)</tt> to the front, and we then have 
the list we want.  
</ul>
<p>Formally, we define the following function:
<pre>
(defun list-append (L1 L2)
  "Append L1 by L2."
  (if (null L1)
      L2
    (cons (first L1) (list-append (rest L1) L2))))
</pre>
An execution trace is the following:
<pre>
USER(73): (list-append '(a b c) '(c d e))
 0: (LIST-APPEND (A B C) (C D E))
   1: (LIST-APPEND (B C) (C D E))
     2: (LIST-APPEND (C) (C D E))
       3: (LIST-APPEND NIL (C D E))
       3: returned (C D E)
     2: returned (C C D E)
   1: returned (B C C D E)
 0: returned (A B C C D E)
(A B C C D E)
</pre>

<p><hr><b>Exercise</b>: LISP defines a function <tt>(butlast <em>L</em>)</tt>
that returns a list containing the same elements in <em>L</em> except
for the last one.  Implement your own version of <tt>butlast</tt>
using linear recursion.  You may assume that <tt>(butlast nil)</tt>
returns <tt>nil</tt>.
<hr>

<h2>Using Lists as Sets</h2>

Formally, lists are ordered sequences.  They differ with sets
in two ways:
<ol>
<li>Sets are unordered, but lists are.  <tt>(a b c)</tt>
and <tt>(c b a)</tt> are two different lists.
<li>
An element either belong to a set or it does not.  There is
no notion of multiple occurrences.  
Yet, a list may contain multiple occurrences of the same element.
<tt>(a b b c)</tt> and <tt>(a b c)</tt> are two different lists.
</ol>
However, one may use lists to approximate sets, although the
performance of such implementation is not the greatest.

<p>We have already seen how we can use the built-in function <tt>member</tt>
to test set membership.  LISP also defines functions like
<tt>(intersection <em>L1</em> <em>L2</em>)</tt>, 
<tt>(union <em>L1</em> <em>L2</em>)</tt>
and <tt>(difference <em>L1</em> <em>L2</em>)</tt> for
boolean operations on sets.  In fact, these functions
are not difficult to implement.  Consider the following
implementation of set intersection:
<pre>
(defun list-intersection (L1 L2)
  "Return a list containing elements belonging to both L1 and L2."
  (cond
   ((null L1) nil)
   ((member (first L1) L2) 
    (cons (first L1) (list-intersection (rest L1) L2)))
   (t (list-intersection (rest L1) L2))))
</pre>
The correctness of the implementation is easy to see.
<em>L1</em> is either an empty set (<tt>nil</tt>) or it is not:
<ul>
<li><em>Case 1</em>: <em>L1</em> is an empty set.<br>
Then its interection with <em>L2</em>
is obviously empty.  
<li><em>Case 2</em>: <em>L1</em> is not empty.<br>
<em>L1</em> has both a <tt>first</tt>
component and a <tt>rest</tt> component.  There are two cases:
either <tt>(first <em>L1</em>)</tt> is a member of <em>L2</em> or it is not.
<ul>
<li><em>Case 2.1</em>: <tt>(first <em>L1</em>)</tt> is a member
of <em>L2</em>.<br>
<tt>(first <em>L1</em>)</tt> belongs to both <em>L1</em>
and <em>L2</em>, and thus belong to their intersection.  Therefore,
the intersection of <em>L1</em> and <em>L2</em> is simply
<tt>(first <em>L1</em>)</tt> plus the intersection of <tt>(rest <em>L1</em>)</tt> and 
<em>L2</em>.
<li><em>Case 2.2</em>: <tt>(first <em>L1</em>)</tt> is not a member
of <em>L2</em>.<br>
Since <tt>(first <em>L1</em>)</tt>
does not belong to <em>L2</em>, it does not belong to the intersection
of <em>L1</em> and <em>L2</em>.  As a result, the intersection of
<em>L1</em> and <em>L2</em> is exactly the intersection of 
<tt>(rest <em>L1</em>)</tt> and <em>L2</em>.
</ul>
</ul>
<p>A trace of executing the function is given below:
<pre>
USER(80): (trace list-intersection)
(LIST-INTERSECTION)
USER(81): (list-intersection '(1 3 5 7) '(1 2 3 4))
 0: (LIST-INTERSECTION (1 3 5 7) (1 2 3 4))
   1: (LIST-INTERSECTION (3 5 7) (1 2 3 4))
     2: (LIST-INTERSECTION (5 7) (1 2 3 4))
       3: (LIST-INTERSECTION (7) (1 2 3 4))
         4: (LIST-INTERSECTION NIL (1 2 3 4))
         4: returned NIL
       3: returned NIL
     2: returned NIL
   1: returned (3)
 0: returned (1 3)
(1 3)
</pre>
<p><hr><b>Exercise</b>: Give a linearly recursive implementation of
<tt>union</tt> and <tt>difference</tt>.
<hr>
</body>
</html>