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

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
1 Object Orientation James Brucker. 2 Smalltalk  Ahead of its time: consistent design and rich library.  Dynamic (like Lisp): variables have no specified.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 6: User-Defined Functions I
LISP primitives on sequences FIRST (or CAR) and REST (or CDR) take lists apart. Consider the list (First day of the semester). * (first '(First day of.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 6: User-Defined Functions I
Guide To UNIX Using Linux Third Edition
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Conditionals and Recursion "To iterate is human, to recurse divine." - L. Peter Deutsch.
Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,
Formal Models of Computation Part II The Logic Model
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Data Abstraction and Mapping.
Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things.
PRACTICAL COMMON LISP Peter Seibel 1.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
Multiple Returns and Extra Parameters “LISP is unique in its capability to return more than one value (or no value at all) from a form. This neatly avoids.
Designing with Procedures 1. Designing a Program with Procedures If the code for your program is going to be less than one page, normally don’t bother;
Iteration Chapters 6 & 7. Iteration in LISP n LISP (unlike Prolog) allows iteration –mapcar, remove-if(-not), count-if, find-if for special purpose iteration.
1 Functions Lecfture Abstraction abstraction is the process of ignoring minutiae and focusing on the big picture in modern life, we are constantly.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Function Design in LISP. Program Files n LISP programs are plain text –DOS extensions vary; use.lsp for this course n (load “filename.lsp”) n Can use.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Introduction to Lisp.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Validating Data Entry The major issue in creating and using data bases, including Excel lists, is to assure that the information stored in the data base.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Working With Objects Tonga Institute of Higher Education.
Top-down approach / Stepwise Refinement & Procedures & Functions.
Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
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.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Unit 10-JavaScript Functions Instructor: Brent Presley.
I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Clojure Macros. Homoiconicity All versions of Lisp, including Clojure, are homoiconic This means that there is no difference between the form of the data.
Chapter 3: Using Methods, Classes, and Objects
Chapter 9 : Assembler Design Options
User-Defined Functions
Chapter 3 Introduction to Classes, Objects Methods and Strings
Phil Tayco Slide version 1.0 Created Oct 9, 2017
Modern Programming Languages Lecture 21 Fakhar Lodhi
Clojure Macros.
Peter Seibel Practical Common Lisp Peter Seibel
Functional Programming: Lisp
Python Review
Common Lisp II.
LISP primitives on sequences
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
Presentation transcript:

Data Abstraction Chapter 6

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) ( )), for example –can assign value to a variable –(setf zachary ‘((zachary young) ( )))

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

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) ( )) –year born is third item of second item –(defun age-in-year (person year) (– year (third (second person))))

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

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)))

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

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

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

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)))

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) …)

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))))

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)

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) ( )

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) ( NIL)

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)

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))

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)

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

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) ( ) –(defun date-year (date) (first date))

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?

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)))

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

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

Getting Year Born n Third item in born field –(assoc ‘born *zachary*) (BORN ( )) –(defun year-born (person) (third (assoc-value ‘born person))) –(year-born *zachary*) 2001 n But still need to know that year comes 3 rd

Dates as Association Lists n Date has year, month and day –( )? 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)))

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)))

Using Association List Objects n (date-year (person-born *zachary*)) 2001 n (name-family (person-name *zachary*)) YOUNG

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)))

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))

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)

Exercise n Rewrite new-person & new-name using back-quote and unquote

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”

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)

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))

Exercise n Defun the writer for a person’s name

Next Time n I/O –chapter 9

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

Test Format n See first test –code writing –code understanding