CONTROL STRUCTURES

CONSTANTS AND VARIABLES

There are two kind of variables, in effect:

REFERENCE

The value of an ordinary variable may be obtained by simply writing the name of the variable as a form to be executed and one way to execute a function is to precede the name of the function by left parentheses followed by its arguments (if any) and closing the initial parentheses.

EXAMPLE: Suppose you had a variable:
a = (the everyday traps)
you can use it in this way:

=


but, if you use it preceded by "(" a right parentheses will see:
=


in the previous case LISP looks for a function named "a" with one parameter.
=


and here in this case LISP looks for a function named "a" without parameters.
In both cases you will see an error message that said "the function A is undefined".

And now, let suppose that a = (the everyday traps) , if what we really want is to produce a list with the word "a" at the beginning of the list and the rest equal to the a list. How would you do that using CONS?

TRY:

=

ASSIGNMENT

Those facilities allow the value of a variable to be altered.


(SETQ {var form } * )

Here, form1 is evaluated and the result is stored in the variable var1, then form2 is evaluated and the result is stored in var2 and so forth.

SETQ returns the last value assigned.

EXAMPLE:

=

In this case you will see three results, the first one is the result of the SETQ, the second one is the result of the evaluation of x and the last one is the evaluation of y. x is set to 6, y is set to nil and SETQ returns nil. Now, try it changing some of the value of the assignment and see the result.

Note, the first assignment is perform before the second form is calculated allowing that form2 can use the new value of var1. Like in the following example:

=


(PSETQ {var form } * )

It is like SETQ form, except that the assignment is made in parallel.
EXAMPLE:

=

Here, we can see the swapping of the variables a and b.
(SET symbol form)

It is almost the same as SETQ, except that it evaluates its first argument and the value of this first arguments must be an atom, the value of this atom is changed to be the value of the evaluation of the form.

EXAMPLE:

= .

the evaluation of 'a returns the atom a and then it is set to the list '(hello).

Besides, this function allows alterations of the value of a dynamic variable.

EXAMPLE:

=

It will either set a to nil or set b to nil, depending on the outcome of the evaluation of (EQUAL a b).

FUNCTIONS

(DEFUN function-name parameter-list commands )

It allows you to define your own command. Takes three arguments; the parameter is a literal atom which appears in the command part of a function and which is worked on by the function's command. The command part may contain and work on several parameters, and each must be declared in parameter-list, which is a list of such atoms. When does not exist parameter is represented by the null list.

EXAMPLE:

Suppose that we want to define the simplest function that give us the second element of a list:

TRY:

(DEFUN 2nd ( l ))
(second `(the capital sins) ) =


And now try to define a function that receive two parameter: a list l and an atom a, and the function must return a list changing the second element of a list by the atom a.
DEFINITION:
(DEFUN Chg2nd ( l a))

INVOCATION:

(Chg2nd `(the capital sins) 'worst ) = .


Function can be defined recursively and in terms of each other. Each definition of a function is of the form:

(DEFINE function-name LAMBDA-expression ) where LAMBDA-expression is of the form: (list-lambda-variables body-of-the-function )

Function-name is an alpha atom. The list-lambda-variables is a list of different alpha atoms enclosed in parentheses which are the dummy variables (arguments) of the function-name. The body-of-the-function is a program whose value is the value of the function. The value of the variable are call by value.


CONDITIONALS

One of the most useful functions in conditional in the description and use of conditional is:
(EQUAL arg1 arg2 ) is a predicate that return T if the two arguments, are names of lists which contains the same elements, or are names of the list that refers to the same memory location, or evaluate to the same atom.

EXAMPLE:

= .

= .



(COND (cond1  command1)
(cond2 command2)
.......
( ) )
This function allows to specify which one of several commands in a lisp statement is to be executed. It is also equivalent to:
if cond1 then command1 else if cond2 then command2 else if ... and so forth. LISP will evaluate the condition part of the first clause, if nil is the result LISP will evaluate the next clause and does not execute the command part. EXAMPLE:
put the condition inside the COND command, using the w and b variables (if need them).
(SETQ w 'white) (SETQ b 'black)
(COND ( '(equal objects))
( '(different objects))) =
.


(IF condition then-part [else-part] )

The IF corresponds to the If-Then-Else construct found in most algebraic programming languages. First condition is evaluated, if the result is not nil, then the then-part is selected otherwise the else-part is selected, and returns the evaluation of the selected part.

This function is equivalent to:

(COND ( condition  then-part )
      ( t          else-part ) )
EXAMPLE:
put the condition inside the IF command, using the w and b variables to get the S-Expression:'(different objects).
(SETQ w 'white) (SETQ b 'black)
(IF '(equal objects) '(different objects))=
.