Download presentation
Presentation is loading. Please wait.
Published byMyron Dorsey Modified over 9 years ago
1
Data Abstraction Chapter 6
2
Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth, to start n Information must be kept in some format –((zachary young) (4 12 2001)), for example –can assign value to a variable –(setf zachary ‘((zachary young) (4 12 2001)))
3
People Information n Each person can get their own variable –(setf *zachary* …) –(setf *alex* …) –(setf *mark* …) –… n May want a list of all the people –(setf *people* (list *zachary* *alex* …)) –global variable convention: *s around name
4
Calculated Information n Age in a given year/on a given date –(age-in-year *zachary* 2012) 11 n Need to extract information from variable –((zachary young) (4 12 2001)) –year born is third item of second item –(defun age-in-year (person year) (– year (third (second person))))
5
Adding More Information n Want to add information about father, mother, spouse, children, … n Add information to the list n Has to be added to end of list –otherwise age-in-year won’t work anymore –(third (second person)) is location specific n Later in list = less efficient to get at
6
Data Abstraction n Needing to know location of information makes coding more difficult n Want to abstract data extraction –function to get the year someone was born –(defun year-born (person) (third (second person))) –(defun age-in-year (person year) (– year (year-born person)))
7
Data Independence n Some functions don’t care how the data is represented n Other functions do n Client wants not to care –functions to get the data they want –functions to create new data items –functions to change values in data items
8
Access Procedures n Standard types of operations on data objects –Reader: extracts information from object –Constructor: creates new object –Writer: replaces information in object n Allows programmer to get on with important code –data representations can be tailored to needs later
9
Access Procedures for People n Get a person’s name –get their family name –get their given name(s) n Get a person’s date of birth –get the year born –get the month born –get the day of the month born
10
Names and Dates n Names and dates are also data with parts –given name(s), last name –year, month, day n Should use access procedures for them, too –get the whole date of birth from the person –get the year born from the date of birth –(defun person-year-born (person) (date-year (person-born person)))
11
Constructors n Function to build an object –given a year, month & day, make a date –what order? –(defun new-date-US (month day year) …) –(defun new-date-UK (day month year)…) –(defun new-date-SI (year month day) …)
12
Environment Variable n Suppose we have a global variable *measure-system* with value US, UK or SI –(defun new-date (d1 d2 d3) (case *measurement-system* (US(new-date-US d1 d2 d3)) (UK(new-date-UK d1 d2 d3)) (SI(new-date-SI d1 d2 d3))))
13
Key Parameters n Key parameters use :names in argument list –arguments can be passed in any order –(new-date :year 2002 :month 3 :day 14) –(new-date :month 3 :day 14 :year 2002) n No need to remember order –do need to remember names of fields –can leave parts of date out –(new-date :month 3 :year 2002)
14
Key Parameters n Use &key in parameter list –everything after it is keyed –parameter name used as key – choose it well > (defun new-date (&key year month day) (list year month day)) NEW-DATE > (new-date :day 14 :month 3 :year 2002) (2002 3 14)
15
Key Arguments n Need to be use names to pass values –doesn’t assume any order n Can be given in any order n Are optional –arguments left off get NIL –(new-date :month 3 :year 2002) (2002 3 NIL)
16
Default Values n As for (other) optional parameters –list with parameter name & default value > (defun new-date (&key (year (this-year)) (month 1) (day 1)) (list year month day)) –default for day and month is 1 –default year is *this* year (need to write this- year function)
17
Exercise n Write constructors for new-name and new- person –name consists of given name and family name –person consists of name and date of birth –(new-person :name (new-name :family ‘lejeune :given ‘jean) :born (new-date :year 1845 :month 10 :day 12))
18
New Fields n Key parameters allow addition of new fields –no need to change old code – if good default values can be found n Add maiden name field to name –(new-name :maiden ‘wolfe :given ‘catherine) n Add a surname field –(new-name :family ‘lejeune :surname ‘briard :given ‘pierre)
19
Writers n Lists can be “surgically” altered –but we don’t need to know how it’s done –(date-year-write (person-born *zachary*) 1900) –(date-year (person-born *zachary*)) 1900 (date-year-write (person-born *zachary*) 2001) –(date-year (person-born *zachary*)) 2001
20
Client vs. Library n Client should not know how data is stored n Access procedures need to know n Many ways to represent information –simplest way: position coded list –(new-date :month 12 :day 4 :year 2001) (2001 12 4) –(defun date-year (date) (first date))
21
Access Procedures n Using positional coding –first = month, second = day, third = year –(or whatever) n Changing positions = changing code –first = year, second = month, third = day –change date-year, date-month & date-day –can we avoid that?
22
Association Lists n AKA a-lists n List of field/value pairs ((height.54) (weight 4.4)) –height is 0.54 (metres) –weight is 4.4 (kilograms) n Can assign this list to a variable (naturally) –(setf *sarah* ‘((height.54) (weight 4.4)))
23
Extracting Field Values n Use assoc to extract values > (assoc ‘height *sarah*) (HEIGHT.54) > (assoc ‘weight *sarah*) (WEIGHT 4.4) n Returns first matching item from list –may have multiple heights; only first one gets used
24
Getting Just the Value n May want a separate function to do that –(defun assoc-value (field-name a-list) (second (assoc field-name a-list))) –(assoc-value ‘height *sarah*).54 –(assoc-value ‘weight *sarah*) 4.4
25
Getting Year Born n Third item in born field –(assoc ‘born *zachary*) (BORN (4 12 2001)) –(defun year-born (person) (third (assoc-value ‘born person))) –(year-born *zachary*) 2001 n But still need to know that year comes 3 rd
26
Dates as Association Lists n Date has year, month and day –(4 12 2001)? April or December? n Association list also disambiguates values –((year 2001) (month 12) (day 4)) –order of fields is unimportant (so long as each field name appears only once) –(defun year-born (person) (assoc-value ‘year (assoc-value ‘born person)))
27
Using Association Lists (setf *zachary* (new-person:name (new-name:given ‘zachary :family ‘young) :born (new-date:day 4 :month 12 :year 2001))) ((name ((given zachary) (family young)) (born ((year 2001) (month 12) (day 4)))
28
Using Association List Objects n (date-year (person-born *zachary*)) 2001 n (name-family (person-name *zachary*)) YOUNG
29
Creating Association Lists n (defun new-date (&key year month day) (list (list ‘year year) (list ‘month month) (list ‘day day))) n (defun new-person (&key name born) (list(list ‘name name) (list ‘born born)))
30
Back-Quotes & Unquotes n Result a bit less obvious than it could be –also a bit wordier n Can build the result using quote & unquote –(defun new-date (&key year month day) `((year,year) (month,month) (day,day))) –(new-date :day 14 :month 3 :year 2002) ((YEAR 2002) (MONTH 3) (DAY 14))
31
Quote/Unquote n Quote-unquote –not forward single quote ('), back-quote (`) –unquote = comma (,) in front of variable name –no comma = quoted –(setf *this-year* 2002) 2002 –`(year,*this-year*) (YEAR 2002)
32
Exercise n Rewrite new-person & new-name using back-quote and unquote
33
Writers n If we use association lists, we can use setf to change values –second item in the a-list element has to be changed –so get the a-list element and set its second element to the new value –(setf (second (assoc field a-list)) new-value) –setf short for “set form”
34
Set Form n Setf can be used to change part of a list –(setf (second list-variable) new-value) –(setf (second (third list-variable)) new-value) n List variable is actually modified –(setf list1 ‘(a b c d)) –(setf (second list1) ‘m) –list1 (A M C D)
35
Writers n Writers all much the same –(defun date-year-write (date new-year) (setf (second (assoc ‘year date)) new-year)) –(setf *today* (new-date :day 14 :month 3)) –(date-write-year *today* 2002) 2002 –*today* ((year 2002) (month 3) (day 14))
36
Exercise n Defun the writer for a person’s name
37
Next Time n I/O –chapter 9
38
One Week From Today n Second midterm n Covers: LISP to end of today’s class –Atoms, lists & math –Function definition –Predicates –List mapping –Association lists, quotes & back-quotes n NO Prolog on this midterm
39
Test Format n See first test –code writing –code understanding
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.