Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

LISP Programming: Function Arg Arg Arg Arg

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

LISP Programming

LISP is one of the simplest computer languages in terms of syntax and semantics, and also one of the
most powerful. It was developed in the mid-1950’s by John McCarthy at M.I.T. as a “LISt Processing
language”. Today, it is used for virtually all Artificial Intelligence programs and is the environment of
choice for applications which require a powerful interactive working environment. LISP presents a
very different way to think about programming from the “algorithmic” languages, such as BASIC,
Fortran and Pascal.
As its name implies, the basis of LISP is a list. One constructs a list by enumerating elements inside a
pair of parentheses. For example, here is a list with four elements (the second element is also a list):
(23 (this is easy) hello 821)
All statements in LISP are function calls with the following syntax: (function arg1 arg2 arg3 … argn). To
evaluate a LISP statement, each of the arguments (possibly functions themselves) are evaluated, and
then the function is invoked with the arguments. For example, (MULT (ADD 2 3) (ADD 1 4 2)) has a
value of 35, since (ADD 2 3) has a value of 5, (ADD 1 4 2) has a value of 7, and (MULT 5 7) has a
value of 35. Some functions have an arbitrary number of arguments; others require a fixed number.
All statements return a value, which is either an atom or a list.

FUNCTION RESULT
(ADD x1 x2 …) sum of all arguments
(MULT x1 x2 …) product of all arguments
(SUB a b) a-b
(DIV a b) a/b
(SQUARE a) a*a
(EXP a n) an
(EQ a b) true if a and b are equal, NIL otherwise
(POS a) true if a is positive, NIL otherwise
(NEG a) true if a is negative, NIL otherwise

Some examples of these functions are as follows:

STATEMENT VALUE
(ADD (EXP 2 3) (SUB 4 1) (DIV 54 4)) 24.5
(SUB (MULT 3 2) (SUB 12 (ADD 2 2))) -2
(ADD (SQUARE 3) (SQUARE 4)) 25

STEMPIA
LISP Programming - Worksheet

1. 05-06 C1 Lisp Programming 4


Evaluate: (DIV (MULT (ADD 2 3) (SQUARE 2)) (SUB (EXP 2 3) (ADD 2 1)))

2. 06-07 C1 Lisp Programming 2


Evaluate: (DIV (MULT (ADD 1 4 5) (SUB 7 2)) (EXP 5 2))

3. 07-08 C1 Lisp Programming 14


Evaluate the following expression:
(ADD (ADD 3 4) (SUB 5 2) (MULT 3 2) (EXP 2 3))

4. 08-09 C1 Lisp Programming 5


Evaluate the following expression:
(DIV (ADD (ADD 3 4) (MULT 4 2)) (SUB 8 5))

5. 12-13 C4 Lisp Programming (5 6)


Simplify the following expression:
(CAR (CAR (CDR (CDR (CDR ‘(1 (2 3) 4 ((5 6) (7 8) 9)))))))

6. 13-14 C4 Lisp Programming (3 (4))


Evaluate: (CAR (CDR (CDR ‘(1 (2) (3 (4))))))

7. 14-15 C4 Lisp Programming (d)


Evaluate the following expression:

(CDR (CAR (CDR (CAR ‘((b (c d) (e f g)) (h (j k)))))))

STEMPIA

You might also like