Material to Understand
- Structure Declarations. Suppose that a person
has a first name, a last name, and an age. Here
is how we could declare such a structure.
(defstruct person ;; defstruct = DEFine STRUCTure
first-name
last-name
(age 0) ;; the default value for age is 0
)
- Structure Creation.
(setf someone (make-person))
;; you can give any field name a default value
;; when you call the constructor for a structure
(setf someone-else (make-person :first-name 'Mary))
- Field Access.
(setf (person-first-name someone) 'Joe)
(setf (person-last-name someone) 'Student)
(setf (person-age someone) 22)
- Other useful predicates.
(person-p someone) ;; return t if someone is a person structure
(describe someone) ;; prints an internal representation
;; of the person instance
Exercises
- Declare a structure called playing-time that can hold
the number of minutes and seconds that a song lasts.
- Instantiate a variable of this structure type and set its
minutes to 5 and its seconds to 3.
- Declare another structure called song that can hold
the name of a song and its playing time (which is a structure
of the type declared above).
- Instantiate a variable of this structure type and set its
name to Les Copains D'abord and its time
to 3 minutes and 5 seconds.
- Print out the name of the song.
- Print out the playing time of the song.
- Declare an array that can contain up to 3 songs.
- Fill in the array with values of your choosing.
- Write a function that will print out the contents
of this array using format statements.
- Use the describe function.
Useful Labor Saving Tip from a Former Student
(defun test ( )
;; Load the appropriate file, in this case, this one.
( load "~account/stuff.l" )
;; Do I want tracing on this?
( trace factorial )
;; Call the desired function with the desired input
( factorial 10 )
)