39
Data Abstraction Data Abstraction Chapter 6 Chapter 6

Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Embed Size (px)

Citation preview

Page 1: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Data AbstractionData Abstraction

Chapter 6Chapter 6

Page 2: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Birthday InformationBirthday Information

Suppose we want to represent information Suppose we want to represent information about peopleabout people– perhaps for a family treeperhaps for a family tree– name & date of birth, to startname & date of birth, to start

Information must be kept in some formatInformation must be kept in some format– ((zachary young) (4 12 2001))((zachary young) (4 12 2001)), for example, for example– can assign value to a variablecan assign value to a variable– (setf zachary ‘((zachary young) (4 12 2001)))(setf zachary ‘((zachary young) (4 12 2001)))

Page 3: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

People InformationPeople Information

Each person can get their own variableEach person can get their own variable– (setf *zachary* …)(setf *zachary* …)– (setf *alex* …)(setf *alex* …)– (setf *mark* …)(setf *mark* …)– ……

May want a list of all the peopleMay want a list of all the people– (setf *people* (list *zachary* *alex* …))(setf *people* (list *zachary* *alex* …))– global variable convention: *s around nameglobal variable convention: *s around name

Page 4: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Calculated InformationCalculated Information

Age in a given year/on a given dateAge in a given year/on a given date– (age-in-year *zachary* 2012)(age-in-year *zachary* 2012)1111

Need to extract information from variableNeed to extract information from variable– ((zachary young) (4 12 2001))((zachary young) (4 12 2001))– year born is third item of second itemyear born is third item of second item– (defun age-in-year (person year)(defun age-in-year (person year)

(– year (third (second person))))(– year (third (second person))))

Page 5: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Adding More InformationAdding More Information

Want to add information about father, Want to add information about father, mother, spouse, children, …mother, spouse, children, …

Add information to the listAdd information to the list Has to be added to Has to be added to endend of list of list

– otherwise otherwise age-in-yearage-in-year won’t work anymore won’t work anymore– (third (second person)) is location specific(third (second person)) is location specific

Later in list = less efficient to get atLater in list = less efficient to get at

Page 6: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Data AbstractionData Abstraction

Needing to know location of information Needing to know location of information makes coding more difficultmakes coding more difficult

Want to abstract data extractionWant to abstract data extraction– function to get the year someone was bornfunction to get the year someone was born– (defun year-born (person)(defun year-born (person)

(third (second person)))(third (second person)))– (defun age-in-year (person year)(defun age-in-year (person year)

(– year (year-born person)))(– year (year-born person)))

Page 7: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Data IndependenceData Independence

Some functions don’t care how the data is Some functions don’t care how the data is representedrepresented

Other functions doOther functions do Client wants not to careClient wants not to care

– functions to get the data they wantfunctions to get the data they want– functions to create new data itemsfunctions to create new data items– functions to change values in data itemsfunctions to change values in data items

Page 8: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Access ProceduresAccess Procedures

Standard types of operations on data objectsStandard types of operations on data objects– Reader: extracts information from objectReader: extracts information from object– Constructor: creates new objectConstructor: creates new object– Writer: replaces information in objectWriter: replaces information in object

Allows programmer to get on with Allows programmer to get on with important codeimportant code– data representations can be tailored to needs data representations can be tailored to needs

laterlater

Page 9: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Access Procedures for PeopleAccess Procedures for People

Get a person’s nameGet a person’s name– get their family nameget their family name– get their given name(s)get their given name(s)

Get a person’s date of birthGet a person’s date of birth– get the year bornget the year born– get the month bornget the month born– get the day of the month bornget the day of the month born

Page 10: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Names and DatesNames and Dates

Names and dates are also data with partsNames and dates are also data with parts– given name(s), last namegiven name(s), last name– year, month, dayyear, month, day

Should use access procedures for them, tooShould use access procedures for them, too– get the whole date of birth from the personget the whole date of birth from the person– get the year born from the date of birthget the year born from the date of birth– (defun person-year-born (person)(defun person-year-born (person)

(date-year (person-born person)))(date-year (person-born person)))

Page 11: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

ConstructorsConstructors

Function to build an objectFunction to build an object– given a year, month & day, make a dategiven a year, month & day, make a date– what order?what order?– (defun new-date-US (month day year) …)(defun new-date-US (month day year) …)– (defun new-date-UK (day month year)…)(defun new-date-UK (day month year)…)– (defun new-date-SI (year month day) …)(defun new-date-SI (year month day) …)

Page 12: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Environment VariableEnvironment Variable

Suppose we have a global variable Suppose we have a global variable *measure-system* with value US, UK or SI*measure-system* with value US, UK or SI– (defun new-date (d1 d2 d3)(defun new-date (d1 d2 d3)

(case *measurement-system*(case *measurement-system*(US(US (new-date-US d1 d2 d3))(new-date-US d1 d2 d3))(UK(UK (new-date-UK d1 d2 d3))(new-date-UK d1 d2 d3))(SI(SI (new-date-SI d1 d2 d3))))(new-date-SI d1 d2 d3))))

Page 13: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Key ParametersKey Parameters

Key parameters use :names in argument listKey parameters use :names in argument list– arguments can be passed in any orderarguments can be passed in any order– (new-date :year 2002 :month 3 :day 14)(new-date :year 2002 :month 3 :day 14)– (new-date :month 3 :day 14 :year 2002)(new-date :month 3 :day 14 :year 2002)

No need to remember orderNo need to remember order– do need to remember names of fieldsdo need to remember names of fields– can leave parts of date outcan leave parts of date out– (new-date :month 3 :year 2002)(new-date :month 3 :year 2002)

Page 14: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Key ParametersKey Parameters

Use &key in parameter listUse &key in parameter list– everything after it is keyedeverything after it is keyed– parameter name used as key – choose it wellparameter name used as key – choose it well

> > (defun new-date (&key year month day)(defun new-date (&key year month day)(list year month day))(list year month day))

NEW-DATENEW-DATE

> > (new-date :day 14 :month 3 :year 2002)(new-date :day 14 :month 3 :year 2002)

(2002 3 14)(2002 3 14)

Page 15: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Key ArgumentsKey Arguments

Need to be use names to pass valuesNeed to be use names to pass values– doesn’t assume any orderdoesn’t assume any order

Can be given in any orderCan be given in any order Are optionalAre optional

– arguments left off get NILarguments left off get NIL– (new-date :month 3 :year 2002)(new-date :month 3 :year 2002)(2002 3 NIL)(2002 3 NIL)

Page 16: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Default ValuesDefault Values

As for (other) optional parametersAs for (other) optional parameters– list with parameter name & default valuelist with parameter name & default value

> > (defun new-date (&key (year (this-year))(defun new-date (&key (year (this-year))(month 1)(month 1)(day 1))(day 1))

(list year month day))(list year month day))– default for day and month is 1default for day and month is 1– default year is *this* year (need to write default year is *this* year (need to write this-this-yearyear function) function)

Page 17: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Exercise Exercise

Write constructors for new-name and new-Write constructors for new-name and new-personperson– name consists of given name and family namename consists of given name and family name– person consists of name and date of birthperson consists of name and date of birth– (new-person :name (new-name :family ‘lejeune(new-person :name (new-name :family ‘lejeune

:given :given ‘jean)‘jean)

:born (new-date :year 1845:born (new-date :year 1845:month 10:month 10:day 12)):day 12))

Page 18: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

New FieldsNew Fields

Key parameters allow addition of new fieldsKey parameters allow addition of new fields– no need to change old code – if good default no need to change old code – if good default

values can be foundvalues can be found Add maiden name field to nameAdd maiden name field to name

– (new-name :maiden ‘wolfe :given ‘catherine)(new-name :maiden ‘wolfe :given ‘catherine) Add a surname fieldAdd a surname field

– (new-name :family ‘lejeune :surname ‘briard (new-name :family ‘lejeune :surname ‘briard :given ‘pierre):given ‘pierre)

Page 19: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

WritersWriters

Lists can be “surgically” alteredLists can be “surgically” altered– but we don’t need to know how it’s donebut we don’t need to know how it’s done– (date-year-write (person-born *zachary*) 1900)(date-year-write (person-born *zachary*) 1900)– (date-year (person-born *zachary*))(date-year (person-born *zachary*))19001900(date-year-write (person-born *zachary*) 2001)(date-year-write (person-born *zachary*) 2001)– (date-year (person-born *zachary*))(date-year (person-born *zachary*))20012001

Page 20: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Client Client vsvs. Library. Library

Client should not know how data is storedClient should not know how data is stored Access procedures need to knowAccess procedures need to know Many ways to represent informationMany ways to represent information

– simplest way: position coded listsimplest way: position coded list– (new-date :month 12 :day 4 :year 2001)(new-date :month 12 :day 4 :year 2001)(2001 12 4)(2001 12 4)– (defun date-year (date)(defun date-year (date)

(first date))(first date))

Page 21: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Access ProceduresAccess Procedures

Using positional codingUsing positional coding– first = month, second = day, third = yearfirst = month, second = day, third = year– (or whatever)(or whatever)

Changing positions = changing codeChanging positions = changing code– first = year, second = month, third = dayfirst = year, second = month, third = day– change change date-yeardate-year, , date-monthdate-month & & date-daydate-day– can we avoid that?can we avoid that?

Page 22: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Association ListsAssociation Lists

AKA AKA a-listsa-lists List of field/value pairsList of field/value pairs

((height .54) (weight 4.4))((height .54) (weight 4.4))– height is 0.54 (metres)height is 0.54 (metres)– weight is 4.4 (kilograms)weight is 4.4 (kilograms)

Can assign this list to a variable (naturally)Can assign this list to a variable (naturally)– (setf *sarah* ‘((height .54) (weight 4.4)))(setf *sarah* ‘((height .54) (weight 4.4)))

Page 23: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Extracting Field ValuesExtracting Field Values

Use Use assocassoc to extract values to extract values> > (assoc ‘height *sarah*)(assoc ‘height *sarah*)(HEIGHT .54)(HEIGHT .54)> > (assoc ‘weight *sarah*)(assoc ‘weight *sarah*)(WEIGHT 4.4)(WEIGHT 4.4)

Returns first matching item from listReturns first matching item from list– may have multiple heights; only first one gets may have multiple heights; only first one gets

usedused

Page 24: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Getting Just the ValueGetting Just the Value

May want a separate function to do thatMay want a separate function to do that– (defun assoc-value (field-name a-list)(defun assoc-value (field-name a-list)

(second (assoc field-name a-list)))(second (assoc field-name a-list)))– (assoc-value ‘height *sarah*)(assoc-value ‘height *sarah*).54.54– (assoc-value ‘weight *sarah*)(assoc-value ‘weight *sarah*)4.44.4

Page 25: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Getting Year BornGetting Year Born

Third item in Third item in bornborn field field– (assoc ‘born *zachary*)(assoc ‘born *zachary*)(BORN (4 12 2001))(BORN (4 12 2001))– (defun year-born (person)(defun year-born (person)

(third (assoc-value ‘born person)))(third (assoc-value ‘born person)))– (year-born *zachary*)(year-born *zachary*)20012001

But still need to know that year comes 3But still need to know that year comes 3 rdrd

Page 26: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Dates as Association ListsDates as Association Lists

Date has year, month and dayDate has year, month and day– (4 12 2001)(4 12 2001)? April or December?? April or December?

Association list also disambiguates valuesAssociation list also disambiguates values– ((year 2001) (month 12) (day 4))((year 2001) (month 12) (day 4))– order of fields is unimportant (so long as each order of fields is unimportant (so long as each

field name appears only once)field name appears only once)– (defun year-born (person)(defun year-born (person)

(assoc-value ‘year (assoc-value ‘born (assoc-value ‘year (assoc-value ‘born person)))person)))

Page 27: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Using Association ListsUsing Association Lists

(setf *zachary*(setf *zachary*(new-person(new-person :name (new-name:name (new-name :given ‘zachary:given ‘zachary

:family ‘young):family ‘young):born (new-date:born (new-date :day 4:day 4

:month 12:month 12:year 2001))):year 2001)))

(( (name (name ((given zachary) (family young))((given zachary) (family young))(born (born ((year 2001) (month 12) (day 4))((year 2001) (month 12) (day 4))

))

Page 28: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Using Association List ObjectsUsing Association List Objects

(date-year (person-born *zachary*))(date-year (person-born *zachary*))

20012001 (name-family (person-name *zachary*))(name-family (person-name *zachary*))

YOUNGYOUNG

Page 29: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Creating Association ListsCreating Association Lists

(defun new-date (&key year month day)(defun new-date (&key year month day)(list (list (list ‘year year)(list ‘year year)

(list ‘month month)(list ‘month month)(list ‘day day)))(list ‘day day)))

(defun new-person (&key name born)(defun new-person (&key name born)(list(list (list ‘name name)(list ‘name name)

(list ‘born born)))(list ‘born born)))

Page 30: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Back-Quotes & UnquotesBack-Quotes & Unquotes

Result a bit less obvious than it could beResult a bit less obvious than it could be– also a bit wordieralso a bit wordier

Can build the result using quote & unquoteCan build the result using quote & unquote– (defun new-date (&key year month day)(defun new-date (&key year month day)

`((year ,year) (month ,month) `((year ,year) (month ,month) (day ,day)))(day ,day)))

– (new-date :day 14 :month 3 :year 2002)(new-date :day 14 :month 3 :year 2002)((YEAR 2002) (MONTH 3) (DAY 14))((YEAR 2002) (MONTH 3) (DAY 14))

Page 31: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Quote/UnquoteQuote/Unquote

Quote-unquoteQuote-unquote– not forward single quote ('), back-quote (`)not forward single quote ('), back-quote (`)– unquote = comma (,) in front of variable nameunquote = comma (,) in front of variable name– no comma = quotedno comma = quoted– (setf *this-year* 2002)(setf *this-year* 2002)20022002– `(year ,*this-year*)`(year ,*this-year*)(YEAR 2002)(YEAR 2002)

Page 32: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

ExerciseExercise

Rewrite new-person & new-name using Rewrite new-person & new-name using back-quote and unquoteback-quote and unquote

Page 33: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

WritersWriters

If we use association lists, we can use setf If we use association lists, we can use setf to change valuesto change values– second item in the a-list element has to be second item in the a-list element has to be

changedchanged– so get the a-list element and set its second so get the a-list element and set its second

element to the new valueelement to the new value– (setf (second (assoc field a-list)) new-value)(setf (second (assoc field a-list)) new-value)– setf short for “set form”setf short for “set form”

Page 34: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Set FormSet Form

Setf can be used to change part of a listSetf can be used to change part of a list– (setf (second (setf (second list-variablelist-variable) ) new-valuenew-value))– (setf (second (third (setf (second (third list-variablelist-variable)) )) new-valuenew-value))

List variable is actually modifiedList variable is actually modified– (setf list1 ‘(a b c d))(setf list1 ‘(a b c d))– (setf (second list1) ‘m)(setf (second list1) ‘m)– list1list1(A M C D)(A M C D)

Page 35: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

WritersWriters

Writers all much the sameWriters all much the same– (defun date-year-write (date new-year)(defun date-year-write (date new-year)

(setf (second (assoc ‘year date)) new-(setf (second (assoc ‘year date)) new-year))year))

– (setf *today* (new-date :day 14 :month 3))(setf *today* (new-date :day 14 :month 3))– (date-write-year *today* 2002)(date-write-year *today* 2002)20022002– *today**today*((year 2002) (month 3) (day 14))((year 2002) (month 3) (day 14))

Page 36: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

ExerciseExercise

Defun the writer for a person’s nameDefun the writer for a person’s name

Page 37: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Next TimeNext Time

I/OI/O– chapter 9chapter 9

Page 38: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

One Week From TodayOne Week From Today

Second midtermSecond midterm Covers: LISP to end of today’s classCovers: LISP to end of today’s class

– Atoms, lists & mathAtoms, lists & math– Function definitionFunction definition– PredicatesPredicates– List mappingList mapping– Association lists, quotes & back-quotesAssociation lists, quotes & back-quotes

NO Prolog on this midtermNO Prolog on this midterm

Page 39: Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,

Test FormatTest Format

See first testSee first test– code writingcode writing– code understandingcode understanding