-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisp.html
More file actions
10391 lines (8435 loc) · 413 KB
/
Copy pathlisp.html
File metadata and controls
10391 lines (8435 loc) · 413 KB
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
<!DOCTYPE html><html><body><title>LISP Tutorial</title>
<h1>LISP Tutorial</h1>
<p>Lisp is the second-oldest high-level programming language after Fortran and has changed a great deal since its early days, and a number of dialects have existed over its history. Today, the most widely known general-purpose Lisp dialects are Common Lisp and Scheme.</p>
<p>Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).</p>
<p>This reference will take you through simple and practical approach while learning LISP Programming language.</p>
<h1>Audience</h1>
<p>This reference has been prepared for the beginners to help them understand the basic to advanced concepts related to LISP Programming language.</p>
<h1>Prerequisites</h1>
<p>Before you start doing practice with various types of examples given in this reference, I'm making an assumption that you are already aware about what is a computer program and what is a computer programming language?</p>
<h1>Execute Lisp Online</h1>
<p>For most of the examples given in this tutorial you will find Try it option, so just make use of this option to execute your Lisp programs at the spot and enjoy your learning.</p>
<p>Try following example using Try it option available at the top right corner of the below sample code box −</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate tryit">
(write-line "Hello World")
</pre>
<title>LISP - Overview</title>
<h1>LISP - Overview</h1>
<p>John McCarthy invented LISP in 1958, shortly after the development of FORTRAN. It was first implement by Steve Russell on an IBM 704 computer.</p>
<p>It is particularly suitable for Artificial Intelligence programs, as it processes symbolic information effectively.</p>
<p>Common Lisp originated, during the 1980s and 1990s, in an attempt to unify the work of several implementation groups, which were successors to Maclisp like ZetaLisp and NIL (New Implementation of Lisp) etc.</p>
<p>It serves as a common language, which can be easily extended for specific implementation.</p>
<p>Programs written in Common LISP do not depend on machine-specific characteristics, such as word length etc.</p>
<h2>Features of Common LISP</h2>
<p>It is machine-independent</p>
<p>It uses iterative design methodology, and easy extensibility.</p>
<p>It allows updating the programs dynamically.</p>
<p>It provides high level debugging.</p>
<p>It provides advanced object-oriented programming.</p>
<p>It provides convenient macro system.</p>
<p>It provides wide-ranging data types like, objects, structures, lists, vectors, adjustable arrays, hash-tables, and symbols.</p>
<p>It is expression-based.</p>
<p>It provides an object-oriented condition system.</p>
<p>It provides complete I/O library.</p>
<p>It provides extensive control structures.</p>
<h2>Applications Built in LISP</h2>
<p>Large successful applications built in Lisp.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>LISP Environment Setup</title>
<h1>LISP - Environment Setup</h1>
<h2>Try it Option Online</h2>
<p>You really do not need to set up your own environment to start learning LISP programming language. Reason is very simple, we already have set up Lisp Programming environment online, so that you can execute all the available examples online at the same time when you are doing your theory work. This gives you confidence in what you are reading and to check the result with different options. Feel free to modify any example and execute it online.</p>
<p>Try following example using <b>Try it</b> option available at the top right corner of the below sample code box −</p>
<p>For most of the examples given in this tutorial, you will find <b>Try it</b> option, so just make use of it and enjoy your learning.</p>
<h2>Local Environment Setup</h2>
<p>If you are still willing to set up your environment for Lisp programming language, you need the following two softwares available on your computer, (a) Text Editor and (b) The Lisp Executer.</p>
<h2>Text Editor</h2>
<p>This will be used to type your program. Examples of few editors include Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi.</p>
<p>Name and version of text editor can vary on different operating systems. For example, Notepad will be used on Windows, and vim or vi can be used on windows as well as Linux or UNIX.</p>
<p>The files you create with your editor are called source files and contain program source code. The source files for Lisp programs are typically named with the extension "<b>.lisp</b>".</p>
<p>Before starting your programming, make sure you have one text editor in place and you have enough experience to write a computer program, save it in a file, finally execute it.</p>
<h2>The Lisp Executer</h2>
<p>The source code written in source file is the human readable source for your program. It needs to be "executed", to turn into machine language so that your CPU can actually execute the program as per instructions given.</p>
<p>This Lisp programming language will be used to execute your source code into final executable program. I assume you have basic knowledge about a programming language.</p>
<p>CLISP is the GNU Common LISP multi-architechtural compiler used for setting up LISP in Windows. The windows version emulates a unix environment using MingW under windows. The installer takes care of this and automatically adds clisp to the windows PATH variable.</p>
<p>It creates a shortcut in the Start Menu by default, for the line-by-line interpreter.</p>
<h2>How to use CLISP</h2>
<p>During installation, <b>clisp</b> is automatically added to your PATH variable if you select the option (RECOMMENDED) This means that you can simply open a new Command Prompt window and type “clisp” to bring up the compiler.</p>
<p>To run a *.lisp or *.lsp file, simply use -</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate tryit">
(write-line "Hello World")
</pre>
<p>For most of the examples given in this tutorial, you will find <b>Try it</b> option, so just make use of it and enjoy your learning.</p>
</blockquote>
<h2>Local Environment Setup</h2>
<p>If you are still willing to set up your environment for Lisp programming language, you need the following two softwares available on your computer, (a) Text Editor and (b) The Lisp Executer.</p>
<h2>Text Editor</h2>
<p>This will be used to type your program. Examples of few editors include Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi.</p>
<p>Name and version of text editor can vary on different operating systems. For example, Notepad will be used on Windows, and vim or vi can be used on windows as well as Linux or UNIX.</p>
<p>The files you create with your editor are called source files and contain program source code. The source files for Lisp programs are typically named with the extension "<b>.lisp</b>".</p>
<p>Before starting your programming, make sure you have one text editor in place and you have enough experience to write a computer program, save it in a file, finally execute it.</p>
<h2>The Lisp Executer</h2>
<p>The source code written in source file is the human readable source for your program. It needs to be "executed", to turn into machine language so that your CPU can actually execute the program as per instructions given.</p>
<p>This Lisp programming language will be used to execute your source code into final executable program. I assume you have basic knowledge about a programming language.</p>
<p>CLISP is the GNU Common LISP multi-architechtural compiler used for setting up LISP in Windows. The windows version emulates a unix environment using MingW under windows. The installer takes care of this and automatically adds clisp to the windows PATH variable.</p>
<p>You can get the latest CLISP for Windows from here - <a href="
http://sourceforge.net/projects/clisp/files/latest/download" rel="nofollow" target="_blank">http://sourceforge.net/projects/clisp/files/latest/download</a></p>
<img src="/lisp/images/lisp_environment_setup.jpg" alt="List Environment Setup" />
<p>It creates a shortcut in the Start Menu by default, for the line-by-line interpreter.</p>
<h2>How to use CLISP</h2>
<p>During installation, <b>clisp</b> is automatically added to your PATH variable if you select the option (RECOMMENDED) This means that you can simply open a new Command Prompt window and type “clisp” to bring up the compiler.</p>
<p>To run a *.lisp or *.lsp file, simply use -</p>
<pre class="prettyprint notranslate">
clisp hello.lisp
</pre>
<title>LISP - Program Structure</title>
<h1>LISP - Program Structure</h1>
<p>LISP expressions are called symbolic expressions or s-expressions. The s-expressions are composed of three valid objects, atoms, lists and strings.</p>
<p>Any s-expression is a valid program.</p>
<p>LISP programs run either on an <b>interpreter</b> or as <b>compiled code.</b></p>
<p>The interpreter checks the source code in a repeated loop, which is also called the read-evaluate-print loop (REPL). It reads the program code, evaluates it, and prints the values returned by the program.</p>
<h2>A Simple Program</h2>
<p>Let us write an s-expression to find the sum of three numbers 7, 9 and 11. To do this, we can type at the interpreter prompt.</p>
<p>LISP returns the result:</p>
<p>If you would like to run the same program as a compiled code, then create a LISP source code file named myprog.lisp and type the following code in it.</p>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<h2>LISP Uses Prefix Notation</h2>
<p>You might have noted that LISP uses <b>prefix notation.</b></p>
<p>In the above program the + symbol works as the function name for the process of summation of the numbers.</p>
<p>In prefix notation, operators are written before their operands. For example, the expression,</p>
<p>will be written as:</p>
<p>Let us take another example, let us write code for converting Fahrenheit temp of 60<sup>o</sup> F to the centigrade scale:</p>
<p>The mathematical expression for this conversion will be:</p>
<p>Create a source code file named main.lisp and type the following code in it.</p>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<h2>Evaluation of LISP Programs</h2>
<p>Evaluation of LISP programs has two parts:</p>
<p>Translation of program text into Lisp objects by a reader program</p>
<p>Implementation of the semantics of the language in terms of these objects by an evaluator program</p>
<p>The evaluation process takes the following steps:</p>
<p>The reader translates the strings of characters to LISP objects or <b>s-expressions.</b></p>
<p>The evaluator defines syntax of Lisp <b>forms</b> that are built from s-expressions. This second level of evaluation defines a syntax that determines which <b>s-expressions</b> are LISP forms.</p>
<p>The evaluator works as a function that takes a valid LISP form as an argument and returns a value. This is the reason why we put the LISP expression in parenthesis, because we are sending the entire expression/form to the evaluator as arguments.</p>
<h2>The 'Hello World' Program</h2>
<p>Learning a new programming language doesn't really take off until you learn how to greet the entire world in that language, right!</p>
<p>So, please create new source code file named main.lisp and type the following code in it.</p>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate">
(+ 7 9 11)
</pre>
<p>LISP returns the result:</p>
<pre class="result notranslate">
27
</pre>
<p>If you would like to run the same program as a compiled code, then create a LISP source code file named myprog.lisp and type the following code in it.</p>
<pre class="prettyprint notranslate tryit">
(write (+ 7 9 11))
</pre>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<pre class="result notranslate">
27
</pre>
<h2>LISP Uses Prefix Notation</h2>
<p>You might have noted that LISP uses <b>prefix notation.</b></p>
<p>In the above program the + symbol works as the function name for the process of summation of the numbers.</p>
<p>In prefix notation, operators are written before their operands. For example, the expression,</p>
<pre class="prettyprint notranslate">
a * ( b + c ) / d
</pre>
<p>will be written as:</p>
<pre class="prettyprint notranslate">
(/ (* a (+ b c) ) d)
</pre>
<p>Let us take another example, let us write code for converting Fahrenheit temp of 60<sup>o</sup> F to the centigrade scale:</p>
<p>The mathematical expression for this conversion will be:</p>
<pre class="prettyprint notranslate">
(60 * 9 / 5) + 32
</pre>
<p>Create a source code file named main.lisp and type the following code in it.</p>
<pre class="prettyprint notranslate tryit">
(write(+ (* (/ 9 5) 60) 32))
</pre>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<pre class="result notranslate">
140
</pre>
<h2>Evaluation of LISP Programs</h2>
<p>Evaluation of LISP programs has two parts:</p>
<ul class="list">
<li><p>Translation of program text into Lisp objects by a reader program</p></li>
<li><p>Implementation of the semantics of the language in terms of these objects by an evaluator program</p></li>
</ul>
<p>The evaluation process takes the following steps:</p>
<ul class="list">
<li><p>The reader translates the strings of characters to LISP objects or <b>s-expressions.</b></p></li>
<li><p>The evaluator defines syntax of Lisp <b>forms</b> that are built from s-expressions. This second level of evaluation defines a syntax that determines which <b>s-expressions</b> are LISP forms.</p></li>
<li><p>The evaluator works as a function that takes a valid LISP form as an argument and returns a value. This is the reason why we put the LISP expression in parenthesis, because we are sending the entire expression/form to the evaluator as arguments.</p></li>
</ul>
<h2>The 'Hello World' Program</h2>
<p>Learning a new programming language doesn't really take off until you learn how to greet the entire world in that language, right!</p>
<p>So, please create new source code file named main.lisp and type the following code in it.</p>
<pre class="prettyprint notranslate tryit">
(write-line "Hello World")
(write-line "I am at 'Tutorials Point'! Learning LISP")
</pre>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<pre class="result notranslate">
Hello World
I am at 'Tutorials Point'! Learning LISP
</pre>
<title>LISP - Basic Syntax</title>
<h1>LISP - Basic Syntax</h1>
<h2>Basic Building Blocks in LISP</h2>
<p>LISP programs are made up of three basic building blocks:</p>
<p>An <b>atom</b> is a number or string of contiguous characters. It includes numbers and special characters.</p>
<p>Following are examples of some valid atoms:</p>
<p>A <b>list</b> is a sequence of atoms and/or other lists enclosed in parentheses.</p>
<p>Following are examples of some valid lists:</p>
<p>A <b>string</b> is a group of characters enclosed in double quotation marks.</p>
<p>Following are examples of some valid strings:</p>
<h2>Adding Comments</h2>
<p>The semicolon symbol (;) is used for indicating a comment line.</p>
<p>For Example,</p>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<h2>Some Notable Points before Moving to Next</h2>
<p>Following are some of the important points to note:</p>
<p>The basic numeric operations in LISP are +, -, *, and /</p>
<p>LISP represents a function call f(x) as (f x), for example cos(45) is written as cos 45</p>
<p>LISP expressions are case-insensitive, cos 45 or COS 45 are same.</p>
<p>LISP tries to evaluate everything, including the arguments of a function. Only three types of elements are constants and always return their own value</p>
<p>Numbers</p>
<p>The letter <b>t,</b> that stands for logical true.</p>
<p>The value <b>nil,</b> that stands for logical false, as well as an empty list.</p>
<h2>Little More about LISP Forms</h2>
<p>In the previous chapter, we mentioned that the evaluation process of LISP code takes the following steps.</p>
<p>The reader translates the strings of characters to LISP objects or <b>s-expressions.</b></p>
<p>The evaluator defines syntax of Lisp <b>forms</b> that are built from s-expressions. This second level of evaluation defines a syntax that determines which s-expressions are LISP forms.</p>
<p>Now, a LISP forms could be.</p>
<p>The evaluator works as a function that takes a valid LISP form as an argument and returns a value. This is the reason why we put the <b>LISP expression in parenthesis,</b> because we are sending the entire expression/form to the evaluator as arguments.</p>
<h2>Naming Conventions in LISP</h2>
<p>Name or symbols can consist of any number of alphanumeric characters other than whitespace, open and closing parentheses, double and single quotes, backslash, comma, colon, semicolon and vertical bar. To use these characters in a name, you need to use escape character (\).</p>
<p>A name can have digits but not entirely made of digits, because then it would be read as a number. Similarly a name can have periods, but can't be made entirely of periods.</p>
<h2>Use of Single Quotation Mark</h2>
<p>LISP evaluates everything including the function arguments and list members.</p>
<p>At times, we need to take atoms or lists literally and don't want them evaluated or treated as function calls.</p>
<p>To do this, we need to precede the atom or the list with a single quotation mark.</p>
<p>The following example demonstrates this.</p>
<p>Create a file named main.lisp and type the following code into it.</p>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate">
hello-from-tutorials-point
name
123008907
*hello*
Block#221
abc123
</pre>
<p>A <b>list</b> is a sequence of atoms and/or other lists enclosed in parentheses.</p>
<p>Following are examples of some valid lists:</p>
<pre class="prettyprint notranslate">
( i am a list)
(a ( a b c) d e fgh)
(father tom ( susan bill joe))
(sun mon tue wed thur fri sat)
( )
</pre>
<p>A <b>string</b> is a group of characters enclosed in double quotation marks.</p>
<p>Following are examples of some valid strings:</p>
<pre class="prettyprint notranslate">
" I am a string"
"a ba c d efg #$%^&!"
"Please enter the following details :"
"Hello from 'Tutorials Point'! "
</pre>
<h2>Adding Comments</h2>
<p>The semicolon symbol (;) is used for indicating a comment line.</p>
<p>For Example,</p>
<pre class="prettyprint notranslate tryit">
(write-line "Hello World") ; greet the world
; tell them your whereabouts
(write-line "I am at 'Tutorials Point'! Learning LISP")
</pre>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<pre class="result notranslate">
Hello World
I am at 'Tutorials Point'! Learning LISP
</pre>
<h2>Some Notable Points before Moving to Next</h2>
<p>Following are some of the important points to note:</p>
<ul class="list">
<li><p>The basic numeric operations in LISP are +, -, *, and /</p></li>
<li><p>LISP represents a function call f(x) as (f x), for example cos(45) is written as cos 45</p></li>
<li><p>LISP expressions are case-insensitive, cos 45 or COS 45 are same.</p></li>
<li><p>LISP tries to evaluate everything, including the arguments of a function. Only three types of elements are constants and always return their own value</p>
<ul class="list">
<li><p>Numbers</p></li>
<li><p>The letter <b>t,</b> that stands for logical true.</p></li>
<li><p>The value <b>nil,</b> that stands for logical false, as well as an empty list.</p></li>
</ul>
</li>
</ul>
<h2>Little More about LISP Forms</h2>
<p>In the previous chapter, we mentioned that the evaluation process of LISP code takes the following steps.</p>
<ul class="list">
<li><p>The reader translates the strings of characters to LISP objects or <b>s-expressions.</b></p></li>
<li><p>The evaluator defines syntax of Lisp <b>forms</b> that are built from s-expressions. This second level of evaluation defines a syntax that determines which s-expressions are LISP forms.</p></li>
</ul>
<p>Now, a LISP forms could be.</p>
<ul class="list">
<li>An Atom</li>
<li>An empty or non-list</li>
<li>Any list that has a symbol as its first element</li>
</ul>
<p>The evaluator works as a function that takes a valid LISP form as an argument and returns a value. This is the reason why we put the <b>LISP expression in parenthesis,</b> because we are sending the entire expression/form to the evaluator as arguments.</p>
<h2>Naming Conventions in LISP</h2>
<p>Name or symbols can consist of any number of alphanumeric characters other than whitespace, open and closing parentheses, double and single quotes, backslash, comma, colon, semicolon and vertical bar. To use these characters in a name, you need to use escape character (\).</p>
<p>A name can have digits but not entirely made of digits, because then it would be read as a number. Similarly a name can have periods, but can't be made entirely of periods.</p>
<h2>Use of Single Quotation Mark</h2>
<p>LISP evaluates everything including the function arguments and list members.</p>
<p>At times, we need to take atoms or lists literally and don't want them evaluated or treated as function calls.</p>
<p>To do this, we need to precede the atom or the list with a single quotation mark.</p>
<p>The following example demonstrates this.</p>
<p>Create a file named main.lisp and type the following code into it.</p>
<pre class="prettyprint notranslate tryit">
(write-line "single quote used, it inhibits evaluation")
(write '(* 2 3))
(write-line " ")
(write-line "single quote not used, so expression evaluated")
(write (* 2 3))
</pre>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<pre class="result notranslate">
single quote used, it inhibits evaluation
(* 2 3)
single quote not used, so expression evaluated
6
</pre>
<title>LISP - Data Types</title>
<h1>LISP - Data Types</h1>
<p>In LISP, variables are not typed, but data objects are.</p>
<p>LISP data types can be categorized as.</p>
<p><b>Scalar types</b> - for example, number types, characters, symbols etc.</p>
<p><b>Data structures</b> - for example, lists, vectors, bit-vectors, and strings.</p>
<p>Any variable can take any LISP object as its value, unless you have declared it explicitly.</p>
<p>Although, it is not necessary to specify a data type for a LISP variable, however, it helps in certain loop expansions, in method declarations and some other situations that we will discuss in later chapters.</p>
<p>The data types are arranged into a hierarchy. A data type is a set of LISP objects and many objects may belong to one such set.</p>
<p>The <b>typep</b> predicate is used for finding whether an object belongs to a specific type.</p>
<p>The <b>type-of</b> function returns the data type of a given object.</p>
<h2>Type Specifiers in LISP</h2>
<p>Type specifiers are system-defined symbols for data types.</p>
<p>Apart from these system-defined types, you can create your own data types. When a structure type is defined using <b>defstruct</b> function, the name of the structure type becomes a valid type symbol.</p>
<h3>Example 1</h3>
<p>Create new source code file named main.lisp and type the following code in it.</p>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<h3>Example 2</h3>
<p>Next let's check the types of the variables used in the previous example. Create new source code file named main. lisp and type the following code in it.</p>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate tryit">
(setq x 10)
(setq y 34.567)
(setq ch nil)
(setq n 123.78)
(setq bg 11.0e+4)
(setq r 124/2)
(print x)
(print y)
(print n)
(print ch)
(print bg)
(print r)
</pre>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<pre class="result notranslate">
10
34.567
123.78
NIL
110000.0
62
</pre>
<h3>Example 2</h3>
<p>Next let's check the types of the variables used in the previous example. Create new source code file named main. lisp and type the following code in it.</p>
<pre class="prettyprint notranslate tryit">
(defvar x 10)
(defvar y 34.567)
(defvar ch nil)
(defvar n 123.78)
(defvar bg 11.0e+4)
(defvar r 124/2)
(print (type-of x))
(print (type-of y))
(print (type-of n))
(print (type-of ch))
(print (type-of bg))
(print (type-of r))
</pre>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<pre class="result notranslate">
(INTEGER 0 281474976710655)
SINGLE-FLOAT
SINGLE-FLOAT
NULL
SINGLE-FLOAT
(INTEGER 0 281474976710655)
</pre>
<title>LISP - Macros</title>
<h1>LISP - Macros</h1>
<p>Macros allow you to extend the syntax of standard LISP.</p>
<p>Technically, a macro is a function that takes an s-expression as arguments and returns a LISP form, which is then evaluated.</p>
<h2>Defining a Macro</h2>
<p>In LISP, a named macro is defined using another macro named <b>defmacro.</b> Syntax for defining a macro is:</p>
<p>The macro definition consists of the name of the macro, a parameter list, an optional documentation string, and a body of Lisp expressions that defines the job to be performed by the macro.</p>
<h3>Example</h3>
<p>Let us write a simple macro named setTo10, which will take a number and set its value to 10.</p>
<p>Create new source code file named main.lisp and type the following code in it.</p>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate">
(defmacro macro-name (parameter-list))
"Optional documentation string."
body-form
</pre>
<p>The macro definition consists of the name of the macro, a parameter list, an optional documentation string, and a body of Lisp expressions that defines the job to be performed by the macro.</p>
<h3>Example</h3>
<p>Let us write a simple macro named setTo10, which will take a number and set its value to 10.</p>
<p>Create new source code file named main.lisp and type the following code in it.</p>
<pre class="prettyprint notranslate tryit">
(defmacro setTo10(num)
(setq num 10)(print num))
(setq x 25)
(print x)
(setTo10 x)
</pre>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<pre class="result notranslate">
25
10
</pre>
<title>LISP - Variables</title>
<h1>LISP - Variables</h1>
<p>In LISP, each variable is represented by a <b>symbol</b>. The variable's name is the name of the symbol and it is stored in the storage cell of the symbol.</p>
<h2>Global Variables</h2>
<p>Global variables have permanent values throughout the LISP system and remain in effect until a new value is specified.</p>
<p>Global variables are generally declared using the <b>defvar</b> construct.</p>
<h3>For example</h3>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is</p>
<p>Since there is no type declaration for variables in LISP, you directly specify a value for a symbol with the <b>setq</b> construct.</p>
<h3>For Example</h3>
<p>The above expression assigns the value 10 to the variable x. You can refer to the variable using the symbol itself as an expression.</p>
<p>The <b>symbol-value</b> function allows you to extract the value stored at the symbol storage place.</p>
<h3>For Example</h3>
<p>Create new source code file named main.lisp and type the following code in it.</p>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is.</p>
<h2>Local Variables</h2>
<p>Local variables are defined within a given procedure. The parameters named as arguments within a function definition are also local variables. Local variables are accessible only within the respective function.</p>
<p>Like the global variables, local variables can also be created using the <b>setq</b> construct.</p>
<p>There are two other constructs - <b>let</b> and <b>prog</b> for creating local variables.</p>
<p>The let construct has the following syntax.</p>
<p>Where var1, var2, ..varn are variable names and val1, val2, .. valn are the initial values assigned to the respective variables.</p>
<p>When <b>let</b> is executed, each variable is assigned the respective value and lastly the <i>s-expression</i> is evaluated. The value of the last expression evaluated is returned.</p>
<p>If you don't include an initial value for a variable, it is assigned to <b>nil.</b></p>
<h3>Example</h3>
<p>Create new source code file named main.lisp and type the following code in it.</p>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is.</p>
<p>The <b>prog</b> construct also has the list of local variables as its first argument, which is followed by the body of the <b>prog,</b> and any number of s-expressions.</p>
<p>The <b>prog</b> function executes the list of s-expressions in sequence and returns nil unless it encounters a function call named <b>return.</b> Then the argument of the <b>return</b> function is evaluated and returned.</p>
<h3>Example</h3>
<p>Create new source code file named main.lisp and type the following code in it.</p>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate tryit">
(defvar x 234)
(write x)
</pre>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is</p>
<pre class="result notranslate">
234
</pre>
<p>Since there is no type declaration for variables in LISP, you directly specify a value for a symbol with the <b>setq</b> construct.</p>
<h3>For Example</h3>
<pre class="prettyprint notranslate">
->(setq x 10)
</pre>
<p>The above expression assigns the value 10 to the variable x. You can refer to the variable using the symbol itself as an expression.</p>
<p>The <b>symbol-value</b> function allows you to extract the value stored at the symbol storage place.</p>
<h3>For Example</h3>
<p>Create new source code file named main.lisp and type the following code in it.</p>
<pre class="prettyprint notranslate tryit">
(setq x 10)
(setq y 20)
(format t "x = ~2d y = ~2d ~%" x y)
(setq x 100)
(setq y 200)
(format t "x = ~2d y = ~2d" x y)
</pre>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is.</p>
<pre class="result notranslate">
x = 10 y = 20
x = 100 y = 200
</pre>
<h2>Local Variables</h2>
<p>Local variables are defined within a given procedure. The parameters named as arguments within a function definition are also local variables. Local variables are accessible only within the respective function.</p>
<p>Like the global variables, local variables can also be created using the <b>setq</b> construct.</p>
<p>There are two other constructs - <b>let</b> and <b>prog</b> for creating local variables.</p>
<p>The let construct has the following syntax.</p>
<pre class="prettyprint notranslate">
(let ((var1 val1) (var2 val2).. (varn valn))<s-expressions>)
</pre>
<p>Where var1, var2, ..varn are variable names and val1, val2, .. valn are the initial values assigned to the respective variables.</p>
<p>When <b>let</b> is executed, each variable is assigned the respective value and lastly the <i>s-expression</i> is evaluated. The value of the last expression evaluated is returned.</p>
<p>If you don't include an initial value for a variable, it is assigned to <b>nil.</b></p>
<h3>Example</h3>
<p>Create new source code file named main.lisp and type the following code in it.</p>
<pre class="prettyprint notranslate tryit">
(let ((x 'a) (y 'b)(z 'c))
(format t "x = ~a y = ~a z = ~a" x y z))
</pre>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is.</p>
<pre class="result notranslate">
x = A y = B z = C
</pre>
<p>The <b>prog</b> construct also has the list of local variables as its first argument, which is followed by the body of the <b>prog,</b> and any number of s-expressions.</p>
<p>The <b>prog</b> function executes the list of s-expressions in sequence and returns nil unless it encounters a function call named <b>return.</b> Then the argument of the <b>return</b> function is evaluated and returned.</p>
<h3>Example</h3>
<p>Create new source code file named main.lisp and type the following code in it.</p>
<pre class="prettyprint notranslate tryit">
(prog ((x '(a b c))(y '(1 2 3))(z '(p q 10)))
(format t "x = ~a y = ~a z = ~a" x y z))
</pre>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is.</p>
<pre class="result notranslate">
x = (A B C) y = (1 2 3) z = (P Q 10)
</pre>
<title>LISP - Constants</title>
<h1>LISP - Constants</h1>
<p>In LISP, constants are variables that never change their values during program execution. Constants are declared using the <b>defconstant</b> construct.</p>
<h2>Example</h2>
<p>The following example shows declaring a global constant PI and later using this value inside a function named <i>area-circle</i> that calculates the area of a circle.</p>
<p>The <b>defun</b> construct is used for defining a function, we will look into it in the <b>Functions</b> chapter.</p>
<p>Create a new source code file named main.lisp and type the following code in it.</p>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate tryit">
(defconstant PI 3.141592)
(defun area-circle(rad)
(terpri)
(format t "Radius: ~5f" rad)
(format t "~%Area: ~10f" (* PI rad rad)))
(area-circle 10)
</pre>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is.</p>
<pre class="result notranslate">
Radius: 10.0
Area: 314.1592
</pre>
<title>LISP - Operators</title>
<h1>LISP - Operators</h1>
<p>An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. LISP allows numerous operations on data, supported by various functions, macros and other constructs.</p>
<p>The operations allowed on data could be categorized as:</p>
<h2>Arithmetic Operations</h2>
<p>The following table shows all the arithmetic operators supported by LISP. Assume variable <b>A</b> holds 10 and variable <b>B</b> holds 20 then:</p>
<p><b><a href="/lisp/lisp_arithmetic_operators.htm">Show Examples</a></b></p>
<h2>Comparison Operations</h2>
<p>Following table shows all the relational operators supported by LISP that compares between numbers. However unlike relational operators in other languages, LISP comparison operators may take more than two operands and they work on numbers only.</p>
<p>Assume variable <b>A</b> holds 10 and variable <b>B</b> holds 20, then:</p>
<p><b><a href="/lisp/lisp_comparison_operators.htm">Show Examples</a></b></p>
<h2>Logical Operations on Boolean Values</h2>
<p>Common LISP provides three logical operators: <b>and, or,</b> and <b>not</b> that operates on Boolean values. Assume <b>A</b> has value nil and <b>B</b> has value 5, then:</p>
<p><b><a href="/lisp/lisp_logical_operators.htm">Show Examples</a></b></p>
<h2>Bitwise Operations on Numbers</h2>
<p>Bitwise operators work on bits and perform bit-by-bit operation. The truth tables for bitwise and, or, and xor operations are as follows:</p>
<p><b><a href="/lisp/lisp_bitwise_operators.htm">Show Examples</a></b></p>
<p>The Bitwise operators supported by LISP are listed in the following table. Assume variable <b>A</b> holds 60 and variable <b>B</b> holds 13, then:</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate">
Assume if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A and B = 0000 1100
A or B = 0011 1101
A xor B = 0011 0001
not A = 1100 0011
</pre>
<title>LISP - Decisions Making</title>
<h1>LISP - Decision Making</h1>
<p>Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.</p>
<p>Following is the general form of a typical decision making structure found in most of the programming languages:</p>
<p>LISP provides following types of decision making constructs. Click the following links to check their detail.</p>
<p><a href="/lisp/lisp_cond_construct.htm">cond</a></p>
<p><a href="/lisp/lisp_if_construct.htm">if</a></p>
<p><a href="/lisp/lisp_when_construct.htm">when</a></p>
<p><a href="/lisp/lisp_case_construct.htm">case</a></p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>LISP - Loops</title>
<h1>LISP - Loops</h1>
<p>There may be a situation, when you need to execute a block of code numbers of times. A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages.</p>
<p>LISP provides the following types of constructs to handle looping requirements. Click the following links to check their detail.</p>
<p><a href="/lisp/lisp_loop_construct.htm">loop</a></p>
<p><a href="/lisp/lisp_loop_for.htm">loop for</a></p>
<p><a href="/lisp/lisp_do.htm">do</a></p>
<p><a href="/lisp/lisp_do_times.htm">dotimes</a></p>
<p><a href="/lisp/lisp_do_list.htm">dolist</a></p>
<h2>Gracefully Exiting From a Block</h2>
<p>The <b>block</b> and <b>return-from</b> allows you to exit gracefully from any nested blocks in case of any error.</p>
<p>The <b>block</b> function allows you to create a named block with a body composed of zero or more statements. Syntax is:</p>
<p>The <b>return-from</b> function takes a block name and an optional (the default is nil) return value.</p>
<p>The following example demonstrates this:</p>
<h2>Example</h2>
<p>Create a new source code file named main.lisp and type the following code in it:</p>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate">
(block block-name(
...
...
))
</pre>
<p>The <b>return-from</b> function takes a block name and an optional (the default is nil) return value.</p>
<p>The following example demonstrates this:</p>
<h2>Example</h2>
<p>Create a new source code file named main.lisp and type the following code in it:</p>
<pre class="prettyprint notranslate tryit">
(defun demo-function (flag)
(print 'entering-outer-block)
(block outer-block
(print 'entering-inner-block)
(print (block inner-block
(if flag
(return-from outer-block 3)
(return-from inner-block 5)
)
(print 'This-wil--not-be-printed))
)
(print 'left-inner-block)
(print 'leaving-outer-block)
t)
)
(demo-function t)
(terpri)
(demo-function nil)
</pre>
<p>When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:</p>
<pre class="result notranslate">
ENTERING-OUTER-BLOCK
ENTERING-INNER-BLOCK
ENTERING-OUTER-BLOCK
ENTERING-INNER-BLOCK
5
LEFT-INNER-BLOCK
LEAVING-OUTER-BLOCK
</pre>
<title>LISP - Functions</title>
<h1>LISP - Functions</h1>
<p>A function is a group of statements that together perform a task.</p>
<p>You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually is so each function performs a specific task.</p>
<h2>Defining Functions in LISP</h2>
<p>The macro named <b>defun</b> is used for defining functions. The <b>defun</b> macro needs three arguments:</p>
<p>Syntax for defun is:</p>
<p>Let us illustrate the concept with simple examples.</p>
<h3>Example 1</h3>
<p>Let's write a function named <i>averagenum</i> that will print the average of four numbers. We will send these numbers as parameters.</p>
<p>Create a new source code file named main.lisp and type the following code in it.</p>
<p>When you execute the code, it returns the following result:</p>
<h3>Example 2</h3>
<p>Let's define and call a function that would calculate the area of a circle when the radius of the circle is given as an argument.</p>
<p>Create a new source code file named main.lisp and type the following code in it.</p>
<p>When you execute the code, it returns the following result:</p>
<p>Please note that:</p>
<p>You can provide an empty list as parameters, which means the function takes no arguments, the list is empty, written as ().</p>
<p>LISP also allows optional, multiple, and keyword arguments.</p>
<p>The documentation string describes the purpose of the function. It is associated with the name of the function and can be obtained using the <b>documentation</b> function.</p>
<p>The body of the function may consist of any number of Lisp expressions.</p>
<p>The value of the last expression in the body is returned as the value of the function.</p>
<p>You can also return a value from the function using the <b>return-from</b> special operator.</p>
<p>Let us discuss the above concepts in brief. Click following links to find details:</p>
<p><a href="/lisp/lisp_optional_parameters.htm">Optional Parameters</a></p>
<p><a href="/lisp/lisp_rest_parameters.htm">Rest Parameters</a></p>
<p><a href="/lisp/lisp_keyword_parameters.htm">Keyword Parameters</a></p>
<p><a href="/lisp/lisp_returning_values_functions.htm">Returning Values from a Function</a></p>
<p><a href="/lisp/lisp_lambda_functions.htm">Lambda Functions</a></p>
<p><a href="/lisp/lisp_mapping_functions.htm">Mapping Functions</a></p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate">
(defun name (parameter-list) "Optional documentation string." body)
</pre>
<p>Let us illustrate the concept with simple examples.</p>
<h3>Example 1</h3>
<p>Let's write a function named <i>averagenum</i> that will print the average of four numbers. We will send these numbers as parameters.</p>
<p>Create a new source code file named main.lisp and type the following code in it.</p>
<pre class="prettyprint notranslate tryit">
(defun averagenum (n1 n2 n3 n4)
(/ ( + n1 n2 n3 n4) 4)
)
(write(averagenum 10 20 30 40))
</pre>
<p>When you execute the code, it returns the following result:</p>
<pre class="result notranslate">
25
</pre>
<h3>Example 2</h3>
<p>Let's define and call a function that would calculate the area of a circle when the radius of the circle is given as an argument.</p>
<p>Create a new source code file named main.lisp and type the following code in it.</p>
<pre class="prettyprint notranslate tryit">
(defun area-circle(rad)
"Calculates area of a circle with given radius"
(terpri)
(format t "Radius: ~5f" rad)
(format t "~%Area: ~10f" (* 3.141592 rad rad))
)
(area-circle 10)
</pre>
<p>When you execute the code, it returns the following result:</p>
<pre class="result notranslate">
Radius: 10.0
Area: 314.1592
</pre>