home *** CD-ROM | disk | FTP | other *** search
-
-
- LISP vs. C
- For Implementing Expert Systems
- February 1987
- Listings 1 & 2
-
- Listing 1.
-
- This example illustrates several aspects of LISP that
- are useful in building expert systems. Symbols are to represent
- information such as room types and as variables to hold specific
- rooms and doors. Lists aggregate related information, doors with
- their corresponding rooms. The MAKE-... functions automatically
- allocate the necessary memory to hold the structures. When a
- structure becomes unaccessible, it is reclaimed by LISP's garbage
- collector.
-
-
- ;;; We will now create a collections of structures that represent
- ;;; 2 rooms a hall and a bathroom. All halls will open onto the
- ;;; hall.
-
- ;;; The structure for information about a room.
- (defstruct room
- type ;the kind of room, eg, BEDROOM
- doors-to-rooms ;doors and the rooms they lead to
- )
-
- ;;; The structure for information about a door.
- (defstruct door
- type ;the type of door
- height ;in inches
- width :in inches
- )
-
- ;;; Make the hall.
- (setf hall (make-room :type 'hall)) ;no connecting rooms yet
-
- ;;; The door from bedroom1 to the hall.
- (setf bedroom1-door (make-door :type 'oak :height 72 :width 40))
-
- ;;; The 1st bedroom with its door to the hall
- (setf bedroom1 (make-room :type 'bedroom
- :doors-to-rooms (list bedroom1-door
- hall)))
-
- ;;; The door from bedroom2 to the hall.
- (setf bedroom2-door (make-door :type 'maple :height 72 :width 40))
-
- ;;; The 2nd bedroom with its door to the hall
- (setf bedroom2 (make-room :type 'master-bedroom
- :doors-to-rooms (list bedroom2-door
- hall)))
-
- ;;; The Bathroom door
- (setf bathroom-door (make-door :type 'pine :height 72 :width 40))
-
- ;;; The bathroom with its door to the hall
- (setf bathroom (make-room :type 'bathroom
- :doors-to-rooms (list bathroom-door
- hall)))
-
- ;;; Now we connect the hall to its corrensponding rooms
- (setf (room-doors-to-rooms hall)
- (list bathroom-door bathroom
- bedroom1-door bedroom1
- bedroom2-door bedroom2))
-
-
-
- Listing 2.
- ;;; An example of the use of a macro. This is from an assembler,
- ;;; it is used to build the necessary databases to assemble the
- ;;; JLE and JNG instructions during PASS2 of the assembler.
- (PASS2 (JLE JNG) #B01111110 CONDITIONAL-BRANCH)
-
- ;;; This is what the above macro translates into to. As you can
- ;;; see, the above form is more clear to the reader as well as
- ;;; easier for the programmer to type.
- (PROGN
- (SETF (GET 'JNG 'BIT-CODE) '126)
- (SETF (GET 'JNG 'PASS2) 'CONDITIONAL-BRANCH)
- (SETF (GET 'JLE 'BIT-CODE) '126)
- (SETF (GET 'JLE 'PASS2) 'CONDITIONAL-BRANCH))
- F (GET 'JNG 'PASS2) 'CONDITIONAL-BRANCH)
- (SETF (GET 'JLE 'BIT-CODE) '126)