Lisp Tutorial

Let us begin with some Lisp commands that we're going to give to our interpreter. You must, first, choose one of the interpreters available in your school. The following dialog is given for LispWorks and uses a Common Lisp syntax.

The mark '>' (or '?' depending on your dialect) indicates that the interpreter is waiting for a user command.

 

Hello World !

A first (easy) example.

? (defun bonjour ()
? 'hello_world)
= bonjour
? (bonjour)
= hello_world

 

Function definition

A function with arguments : PMS

? (defun pms (a b)
? (- (* a b) (+ a b)) )
= pms
? (pms 3 4)
= 5

 

Setq and eval manipulation

Lisp is powerful :

? (setq A 4)
= 4
? A
= 4
? (setq A '(+ 6 4))
= (+ 6 4)
? A
= (+ 6 4)
? (eval A)
= 10

A program that manipulates a program :

? (rplaca A '-)
= (- 6 4)
? A
= (- 6 4)
? (eval A)
= 2

Now, a data becomes a program :

? (setq B '(le chien mange)
? )
= (le chien mange)
? B
= (le chien mange)
? (eval B)
** eval : variable indefinie : chien
? (setq B (la fille chante))
** eval : fonction indefinie : la

Let us create the (artificial) function la!

? (defun la (nom verbe)
? (list
? 'la
? nom
? 'rit))
= la
? (la femme travaille)
** eval : variable indefinie : femme
? (la 'femme 'travaille)
= (la femme rit)
?

It does work!


ICAM Toulouse - Last update: 06-Sep-2013