Page 66 - DCAP310_INTRODUCTION_TO_ARTIFICIAL_INTELLIGENCE_AND_EXPERT_SYSTEMS
P. 66

Introduction to Artificial Intelligence & Expert Systems




                    Notes          4.3 Basic List Manipulation Functions in LISP

                                   Basic List Manipulation Functions in LISP are as follows:

                                   4.3.1 List Manipulation—Step 1

                                   As you are probably well aware, LISP stands for “List Processing”. (Not “Lost in Stupid
                                   Parenthesis”). A list is a group of elements consisting of any data type and is stored as a single
                                   variable. A list can contain any number of Reals, Integers, Strings, Variables and even other
                                   Lists.
                                   Let’s have a look at a list. Type this:
                                     (setq pt1 (getpoint “\nChoose a Point: “))
                                   AutoLisp should return something like this:
                                     (127.34 35.23 0.0)
                                   Fine, you say, I’ve got a list but what do I do with it? AutoLisp has many functions available to
                                   manipulate lists. Let’s have a look at them.
                                   Car


                                   The primary command for taking a list apart is the “Car” function. This function returns the first
                                   element of a list. (The x coordinate.)
                                   For example,
                                     (setq a (car pt1))
                                   Would return:
                                     (127.34)

                                   Cdr

                                   This function returns the second element plus the remaining elements of a list. For example,
                                     (setq b (cdr pt1))
                                   Would return:
                                     (35.23 0.0)
                                   But what if we only wanted the second element? We could write:

                                     (setq b (car (cdr pt1)))
                                   But there is a better way. AutoLisp has provided the “Cadr” function which is basically an
                                   abbreviation of a nested command.

                                   Cadr

                                   This returns the second element of a list. (The y coordinate)
                                     (setq b (cadr pt1))
                                   This would return:
                                     (35.23)
                                   Likewise, there is another abbreviated function to return the third element.





          60                                LOVELY PROFESSIONAL UNIVERSITY
   61   62   63   64   65   66   67   68   69   70   71