Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structures Structures in LISP are analogous to that in C. To create a structure, use the following syntax: (defstruct Name Slot1 Slot2 Slot3) An example.

Similar presentations


Presentation on theme: "Structures Structures in LISP are analogous to that in C. To create a structure, use the following syntax: (defstruct Name Slot1 Slot2 Slot3) An example."— Presentation transcript:

1 Structures Structures in LISP are analogous to that in C. To create a structure, use the following syntax: (defstruct Name Slot1 Slot2 Slot3) An example would be: (defstruct Submarine Course Speed Depth )

2 Creating instances To create an instance of the structure Submarine, do as follows: (make-Submarine :Course 90 :Speed 5.5 :Depth 200) This create and instance of Submarine and intializes the slots to the specified value. You could assign the instance to a variable: (setq Sub1 (make-Submarine :Course 90 :Speed 5.5 :Depth 200))

3 Default values for slots You can also assign default values for the slots when you define the structure. For example, (defstruct Submarine (Course 180) (Speed 10.0) (Depth 250) ) You can check the default values by (make-Submarine)

4 Accessing and setting slot value To access the slot value of an instance of a structure, (StructureName-SlotName InstanceName) To read the value of the slot Course of Sub1, do as follows: (Submarine-Course Sub1) To set/modify the value of a slot, use the function setf For example, to set the slot Course of Sub1 to 270, you could do this: (setf (Submarine-Course Sub1) 270)

5 Type-of, typep If you have an instance of a particular structure, say for example Sub1, You could obtain the name of the structure by using the function type-of. For example, (type-of Sub1) typep is a predicate that tests for the type of aprticular structure instance. For example (typep Sub1 ‘Submarine) The above expression will return t since Sub1 is an instance of Submarine.

6 Simple inheritance (1) Let’s say that we wish to create another structure called Kilo, and we want it to inherit the definition of Submarine. This is can be done as follows: (defstruct (Kilo (:include Submarine)) (SubType ‘Assault) ) This means that Kilo would inherit the slots defined by Submarine. It also adds another slot called SubType, and initializes it to ‘Assault.

7 Simple inheritance (2) To create an instance of Kilo, you would do as so: (setq Kilo1 (make-Kilo :Speed 62)) Then to access the value of Speed : (Kilo-Speed Kilo1) (Submarine-Speed Kilo1) Testing for structure type: (type-of Kilo1) (typep Kilo1 ‘Kilo) (typep Kilo1 ‘Submarine)


Download ppt "Structures Structures in LISP are analogous to that in C. To create a structure, use the following syntax: (defstruct Name Slot1 Slot2 Slot3) An example."

Similar presentations


Ads by Google