Introduction To Lisp
Introduction To Lisp
Introduction To Lisp
It is expression-based.
G2
AutoCad
Igor Engraver
Yahoo Store
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.
A Simple Program
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.
(+ 7 9 11)
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.
(write (+ 7 9 11))
When you click the Execute button, or type Ctrl+E, LISP executes it
immediately and the result returned is −
27
In the above program the + symbol works as the function name for the
process of summation of the numbers.
In prefix notation, operators are written before their operands. For example,
the expression,
a * ( b + c ) / d
will be written as −
(/ (* a (+ b c) ) d)
Let us take another example, let us write code for converting Fahrenheit
temp of 60o F to the centigrade scale −
(60 * 9 / 5) + 32
Create a source code file named main.lisp and type the following code in it.
When you click the Execute button, or type Ctrl+E, LISP executes it
immediately and the result returned is−
140
The evaluator defines syntax of Lisp forms that are built from s-
expressions. This second level of evaluation defines a syntax that
determines which s-expressions are LISP forms.
So, please create new source code file named main.lisp and type the
following code in it.
When you click the Execute button, or type Ctrl+E, LISP executes it
immediately and the result returned is −
Hello World
atom
list
string
hello-from-tutorials-point
name
123008907
*hello*
Block#221
abc123
( i am a list)
(a ( a b c) d e fgh)
( )
" I am a string"
Adding Comments
The semicolon symbol (;) is used for indicating a comment line.
For Example,
When you click the Execute button, or type Ctrl+E, LISP executes it
immediately and the result returned is −
Hello World
o Numbers
o The value nil, that stands for logical false, as well as an empty
list.
The evaluator defines syntax of Lisp forms that are built from s-
expressions. This second level of evaluation defines a syntax that
determines which s-expressions are LISP forms.
An Atom
An empty or non-list
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.
At times, we need to take atoms or lists literally and don't want them
evaluated or treated as function calls.
To do this, we need to precede the atom or the list with a single quotation
mark.
Create a file named main.lisp and type the following code into it.
(write (* 2 3))
When you click the Execute button, or type Ctrl+E, LISP executes it
immediately and the result returned is −
single quote used, it inhibits evaluation
(* 2 3)
single quote not used, so expression evaluated
6