第拾叄章學習 Lisp 3rd Edition, Winston & Horn

STRUCTURES
----------------------------------------------
STRUCTURE TYPES FACILITATE DATA ABSTRACTION

(defstruct person
  (sex nil)                ;Default value is NIL.
  (personality 'nice))     ;Default value is NICE.

(defstruct <structure name>
  (<field name 1> <default value 1>)
  (<field name 2> <default value 2>)
  ...
  (<field name n> <default value n>))

(setf person-instance-1 (make-person))

(setf person-instance-2 (make-person :sex 'female))
--------------------------------------------------------------

STRUCTURE TYPES ENABLE STORAGE IN PROCEDURALLY INDEXED PLACES

* (person-sex person-instance-2)
FEMALE

* (person-personality person-instance-2)
NICE

* (setf (person-sex person-instance-1) 'female)
* (person-sex person-instance-1)
FEMALE
--------------------------------------------------------------

INDIVIDUAL STRUCTURE TYPES ARE NEW DATA TYPES

* (person-p person-instance-1)
T
* (person-p '(this is a list -- not a person instance))
NIL

* (setf (person-surname person-instance-1) 'winston)
ERROR

題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題

13-1: Define a structure type for rocks that contains field
for a rock's color, size, and worth.  Arrange for the default
color to be gray, the default size to be pebble, and the
default worth to be nothing.
-------------------------------------------------------------
13-2: Use MAKE-ROCK, automatically defined in the previous
problem, to create a gold nugget, with gold color, default
size, and high worth.  Assign the instance representing the
gold nugget to the symbol HIGH-HOPES.
----------------------------------------------------------------
13-3: Too bad, your rock turned out to be fool's gold.  Modify
the instance assigned to HIGH-HOPES such that the worth is nothing.
Then make a table showing the values and how the values got there
for all the fields in the instance.
-----------------------------------------------------------------

ONE STRUCTURE TYPE CAN INCLUDE THE FIELDS OF ANOTHER

(defstruct employee
  (length-of-service 0)
  (payment 'salary))

(defstruct (hacker (:include employee))
  (preferred-language 'lisp))

* (setf employee-example (make-employee))
* (setf hacker-example (make-hacker))
* (employee-length-of-service employee-example)
0
* (hacker-length-of-service hacker-example)
0

* (employee-length-of-service hacker-example)
0

* (employee-payment hacker-example)
SALARY
* (hacker-preferred-language hacker-example)
LISP

(defstruct (salesperson (:include employee (payment 'commission)))
  (preferred-car 'mercedes))

* (setf salesperson-example (make-salesperson)
* (employee-payment hacker-example)
SALARY
* (employee-payment salesperson-example)
COMMISSION

題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題

13-4: Define a structure type for studs of the kind that houses
are built of, with default of 2*4, a default length of 8, and a 
default strength of MEDIUM.  Then define a structure type for
oak studs in terms of ordinary studs.  Indicate that the strength
of oak studs is high.
-------------------------------------------------------------------

DESCRIBE PRINTS DESCRIPTIONS

* (describe person-instance-1)
It is a Named Instance of type PERSON.
   Its field names and values are:
    SEX - FEMALE
    PERSONALITY -NICE

* (describe person-instance-2)
It is a Named Instance of type PERSON.
   Its field names and values are:
    SEX - FEMALE
    PERSONALITY -NICE

解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解

13-1:
(defstruct rock
  (color 'gray)
  (size 'pebble)
  (worth 'nothing))
-------------------------------------
13-2:
(setf high-hopes (make-rock :color 'gold :worth 'high))
--------------------------------------------------
13-3:
(setf (rock-worth high-hopes) 'nothing)

Field        Value        How it Got There
Color        Gold         Supplied in MAKE-ROCK form.
Size         Pebble       Supplied in DEFSTRUCT form.
Worth        Nothing      Supplied in SETF form.
------------------------------------------------------
13-4:
(defstruct stud
  (size '2*4)
  (length 8)
  (strength 'medium))

(defstruct (oak-stud (:include stud (strength 'high))))

你可能感兴趣的:(erlang,Scheme,haskell,lisp,clojure)