User Tools

Site Tools


lisp:quickstart

LISP Quickstart

This is only short tutorial to programming in LISP. This quickstart does not contain many functions, it shows how to use command line and write simple programs.

Running CLISP, writing, loading and compiling programs

To run CLIPS just type in clisp in console. It will start Common Lisp command prompt, similar to *nix-like shell or DOS prompt. To exit CLIPS write (exit) or (quit) in command line.
You may edit LISP programs in any ASCII editor, then save as *.lisp file, for example my-first-program.lisp. Function

(load "my-first-program.lisp")

will load saved file to CLISP environment and then you will be able to use functions from the file.

Lisp is both interpreter and compiler. If You type in code in command line it is interpreted. You may compile written functions or whole LISP files using

[2]> (compile 'function-name)

for functions written in command line or

[3]> (compile-file "my-file.lisp")

for files with LISP code. You will get my-file.fas file You can load into environment

[4]> (load "my-file")

Why compile files in LISP? Because of speed:

(time (factorial 10000))                                                                                         
Real time: 0.531118 sec.                                                                                              
Run time: 0.47203 sec.                                                                                                
Space: 69660464 Bytes                                                                                                 

without compilation

(time(factorial 10000))
Real time: 0.472655 sec.    
Run time: 0.46403 sec.      
Space: 69660464 Bytes       

with compiled function. Do not try this in C or C++ ;)

Evaluating simple expressions

An expression is something that is evaluated, by which we mean, submitted to LISP and executed. All things that are evaluated will return a value.
Numbers in LISP are expressions.The value of a number is itself.

[1]> 3
3
[2]> -5
-5
[3]> 3,5
3
[4]> 3.5
3.5
[5]> 4/68    ;this is NOT "divide 4 by 68"!
1/17
[6]> 3e34    ;3^34
3.0E34
[7]> #C(2 9)    ;a complex number 2+9i
#C(2 9)

Strings are expressions. Just like a numbers and characters, the value of a string is itself.

[8]> "Hello World!"
"Hello World!"

In Lisp, the special constant nil (case insensitive) all by itself represents “false”. nil evaluates to itself.

Every other expression but nil is considered to be “true”. However, Lisp also provides an “official” constant which represents “true”, for your convenience. This is t (also case-insensitive). t also evaluates to itself.

Lists

The special form quote can be used to bypass the evaluation of its argument. quote takes a single argument, and instead of evaluating that argument, it simply returns the argument as you had typed it as data.

[9]> (quote (1 2 3 (one two) 4))
(1 2 3 (ONE TWO) 4)
[10]> '(1 2 3 (one two) 4)
(1 2 3 (ONE TWO) 4)

Note that (quote (1 2))'(1 2)

Evaluating lists as functions

Everything in LISP is a list(except atoms - strings, numbers and booleans). Lists are expressions, it means that lists return a value when evaluated. When Lisp evaluates a list, it first examines (but does not evaluate) the symbol at the beginning of the list. Usually this symbol is associated with a function. Lisp looks up this function. The evaluation procedure is recursive.

[11]> (sqrt 2)
1.4142135
[12]> (* (log (sin 0.84) 3) (length "Hello World!"))
-3.220611
[13]> (my-fun 1 2)
 
*** - EVAL: undefined function MY-FUN

Variables

Except when they're at the head of a list, symbols are also expressions. When it's not the head of a list, a symbol represents a variable. When evaluated, a symbol will return the value of a variable. Variables are set with the macro setf.

[14]> (setf x (* 2 3))
6
[15]> x
6
[16]> (+ pi (setf my-pi 3.14) pi)
9.423185

To declare local variables use special form let

(let ( declaration1 declaration2 ... )
     expr1
     expr2
     ... )

For example:

[17]> (let ((x 3) (y (+ 4 9))) (* x y))
39

Conditionals

Basic control structure is special form if

(if test-expression
    then-expression
    optional-else-expression)
[18]> (if (= 2 2) (if (> 3 2) 4 6) 9) ;if 2==2, then if 3>2, then return 4 else return 6 else return 9
4

if only allows one test-expression, one then-expression, and one optional-else-expression. What if you want to do three things in the then-expression? You need to make a block (a group of expressions executed one-by-one). Blocks are made with the special form progn, which takes the form:

(progn expr1 expr2 expr3 ...)

progn can take any number of expressions, and evaluates each of its expressions in order. progn then returns the value of the last expression.

[19]> (if (> 3 2)
           (progn (print "first") (print "second") 9)
           (+ 1 2))
 
"first"
"second"
9

Defining functions

In Lisp, functions are created by calling a function-making macro. This macro is called defun.

A simple version of defun takes the following general form:

(defun function-name-symbol 
            (param1 param2 param3 ...)
    expr1
    expr2
    expr3
    ... )

For example:

[20]> (defun print-reverse (str)
         (print str)
         (print (reverse str))
         1)
PRINT-REVERSE
[21]> (print-reverse "Hello World!")
 
"Hello World!"
"!dlroW olleH"
1
[22]> (defun factorial (n)
          (let ((sum 1))
            (dotimes (x n)
              (setf sum (* sum (1+ x))))
            sum))
FACTORIAL
[23]> (factorial 6)
720
lisp/quickstart.txt · Last modified: 2022/01/17 18:29 (external edit)