Page 72 - DCAP310_INTRODUCTION_TO_ARTIFICIAL_INTELLIGENCE_AND_EXPERT_SYSTEMS
P. 72

Introduction to Artificial Intelligence & Expert Systems




                    Notes          The second argument, (1st-number 2nd-number), is a list of what are called the parameters.
                                   These are symbols representing the numbers we are going to refer to later in the procedure.
                                   The rest of the definition, (/(+ 1st-number 2nd-number) 2), is called the body of the procedure.
                                   It tells LISP how to calculate the value of the procedure, in this case sum the numbers and divide
                                   by 2.

                                   The words you use for the parameters are arbitrary, as long as they match the words you use in
                                   the procedure definition. So you could equally well have written the average procedure as
                                   follows:
                                     (defun average (a b)
                                       (/(+ a b) 2))
                                   To define it we can type the definition at the LISP prompt:
                                     CL-USER > (defun average (1st-number 2nd-number)
                                       (/(+ 1st-number 2nd-number) 2))
                                     AVERAGE
                                   Alternatively you could type the definition into the LISP Editor and select Compile Buffer to
                                   evaluate it.
                                   We can try it out with:
                                     CL-USER 13 > (average 7 9)
                                     8
                                   or:
                                     CL-USER 14 > (average (+ 2 3) (+ 4 5))
                                     7
                                   Remember that the arguments are evaluated before they are given to the procedure.

                                   Procedures Without Parameters

                                   A procedure doesn’t have to have any parameters. Here’s a procedure dice that returns a random
                                   dice throw from 1 to 6:
                                     (defun dice ()
                                       (+ 1 (random 6)))
                                   To call the procedure you simply write:
                                     CL-USER 10 > (dice)
                                     5


                                          Example 1:
                                   Objective: Write a program showing use of LISP arithmetic functions.
                                   Solution:

                                   Function Call                 Value Returned
                                   (+ 3 5 8 4)                         20
                                   (- 10 12)                           -2

                                   (* 2 3 4)                           24
                                   (/25 2)                            12.5






          66                                LOVELY PROFESSIONAL UNIVERSITY
   67   68   69   70   71   72   73   74   75   76   77