1 / 16

Structures

Structures.

psantos
Download Presentation

Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structures “The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination. Few media of creation are so flexible, so easy to polish and rework, so readily capable of realizing grand conceptual structures.” -Frederick P Brooks Jr

  2. Structures • Like struct’s in C/C++ • In other words, like objects, but without methods • Provides a way to define new data types having a collection of labeled fields, each with a default value.

  3. Defstruct (1) • Used to define a new structure: (defstruct struct-name (field-1 default-1) (field-2 default-2) … (field-n default-n)) • Example (defstruct starship (name nil) (speed 0) (condition 'green) (shields 'down))

  4. Defstruct (2) • A defstruct does a bunch of things • Creates constructor procedure • Creates accessors • Generalizes setf for the new type • Creates predicate for testing if something is of the new type

  5. Constructor >(make-starship) #S(STARSHIP NAME NIL SPEED 0 CONDITION GREEN SHIELDS DOWN) • Exact representation depends on implementation • Use keywords to override default values of fields >(make-starship :name "Enterprise" :speed 12 :condition 'red-alert :shields 'damaged) #S(STARSHIP NAME "Enterprise" SPEED 12 CONDITION RED-ALERT SHIELDS DAMAGED)

  6. Type Predicate >(defvar x (make-starship)) X >(starship-p x) T >(type-of x) STARSHIP >(type-of 'foo) SYMBOL >(starship-p 'foo) NIL

  7. Accessors • There is an accessor defined for each field in the defstruct >(starship-name x) NIL >(starship-speed x) 0 >(starship-condition x) GREEN >(starship-shields x) DOWN

  8. Modifying • Use accessor functions together with setf to modify fields >(setf (starship-name x) "Red Dwarf") "Red Dwarf" >x #S(STARSHIP NAME "Red Dwarf" SPEED 0 CONDITION GREEN SHIELDS DOWN)

  9. Redefining Structures • LISP will let you redefine structures with additional defstruct calls, but instances made under the old structure definition may no longer work correctly. It’s best to define a structure only once per session.

  10. Describe takes any kind of LISP object and provides an informative description of it (implementation dependent) >(describe 'foo) FOO - internal symbol in USER package >(describe 7) 7 - fixnum (32 bits) >(describe x) Structure of type STARSHIP Byte:[Slot Type]Slot Name :Slot Value 0:NAME :"Red Dwarf" 4:SPEED :0 8:CONDITION :GREEN 12:SHIELDS :DOWN

  11. Defstruct Options • defstruct takes some further options • (:conc-name symbol) specifies the prefix of all accessor function names >(defstruct (square (:conc-name sq-)) (side 0)) SQUARE >(sq-side (make-square)) 0 • (:constructor symbol <arg list>) specifies a constructor name and optional order of arguments for constructor >(defstruct (rectangle (:constructor rect (length width))) (length 0) (width 0)) RECTANGLE >(rect 4 5) #S(RECTANGLE LENGTH 4 WIDTH 5) • (:predicate symbol) specifies the name of the predicate that tests to see if an item is of the type of the structure >(defstruct (starship (:predicate ssp)) (name nil) (speed 0)) STARSHIP >(ssp (make-starship)) T

  12. Inheritance • Structures can also inherit fields: so one structure can be defined as an extension of another • Done with (:include parent) >(defstruct person (name nil) (age 0)) PERSON >(defstruct (student (:include person)) (major "undecided")) STUDENT >(make-student) #S(STUDENT NAME NIL AGE 0 MAJOR "undecided")

  13. Symbol Property Lists • Each symbol can have both a value and a function definition (this is why you can have variables named list or length in your functions) • Another way to store several fields in a variable is through a symbol’s property list, which is the third thing every symbol possesses • Property lists provide key/value info • Not association lists, but lists of alternating keys and values

  14. Get • To get the value for a key in a symbol’s property list, use get (get symbol key) • Update/add properties by using setf with get >(get 'car 'color) NIL >(setf (get 'car 'color) 'red) COLOR >(get 'car 'color) RED

  15. Remprop • remprop is used to remove a property • Returns T if successful, NIL otherwise >(remprop 'car 'color) T >(get 'car 'color) NIL

  16. Symbol-Plist • symbol-plist lets you look at the entire property list >(setf (get 'dog 'color) 'black) COLOR >(setf (get 'dog 'breed) 'scotty) BREED >(symbol-plist 'dog) (BREED SCOTTY COLOR BLACK)

More Related