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.

Slides:



Advertisements
Similar presentations
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Advertisements

Final and Abstract Classes
UML (cont.) “The Unified Modeling Language User Guide” by G. Booch, J. Rumbaugh and I. Jacobson ● Classes ● Relationships ● Class diagrams ● Examples.
Copyright © Texas Education Agency, Computer Programming Class Basics.
Generics "It is easy to study the rules of overloading and of templates without noticing that together they are one of the keys to elegant and efficient.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Inheritance (notes for 10/26 lecture). Inheritance Inheritance is the last of the relationships we will study this semester. Inheritance is (syntactically)
Structures Structures in LISP are analogous to that in C. To create a structure, use the following syntax: (defstruct Name Slot1 Slot2 Slot3) An example.
ASP.NET Programming with C# and SQL Server First Edition
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
TCU CoSc Introduction to Programming (with Java) Getting to Know Java.
UML Basics & Access Modifier
Why Analysis Process Refer to earlier chapters Models what the system will do makes it easier for understanding no environment considered (hence, system.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
PRACTICAL COMMON LISP Peter Seibel 1.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Mitthögskolan 10/8/ Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it.
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.
Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.
Let’s Celebrate – Visual Basic 2008 Professor Corinne Hoisington.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Where are we? Alice is a “toy” language Fundamentals are exactly like “real”
Local Variables Garbage collection. © 2006 Pearson EducationParameters2 of 10 Using Accessors and Mutators Together What if you want to get a property.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Object Oriented Analysis and Design Class and Object Diagrams.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Salman Marvasti Sharif University of Technology Winter 2015.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
1 OOP - An Introduction ISQS 6337 John R. Durrett.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
When the program is first started a wizard will start to setup your Lemming App. Enter your company name and owner in the fields designated “Company Name”
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
OOP: Encapsulation &Abstraction
Classes C++ representation of an object
Intelligent Agents Lecture 3-2 October 14th, 1999 CS250 Lecture 2-1
Understand the Fundamentals of Classes
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects Outline Test-Driving the Microwave Oven Application Designing.
Imperative Data Structures
More About Inheritance & Interfaces
Assessment – Java Basics: Part 1
Defining Functions with DEFUN
Abstraction and Repetition
CIS 199 Final Review.
Classes C++ representation of an object
Final and Abstract Classes
2.1 Introduction to Object-Oriented Programming
Common Lisp II.
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
Presentation transcript:

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 exertion of the imagination. Few media of creation are so flexible, so easy to polish and rework, so readily capable of realizing grand conceptual structures.” -Frederick P Brooks Jr

Structures Like struct ’s in C/C++ In other words, like objects, but without methods Provides a way to define new data types having a collection of labeled fields, each with a default value.

Defstruct (1) Used to define a new structure: (defstruct struct-name (field-1 default-1) (field-2 default-2) … (field-n default-n)) Example (defstruct starship (name nil) (speed 0) (condition 'green) (shields 'down))

Defstruct (2) A defstruct does a bunch of things – Creates constructor procedure – Creates accessors – Generalizes setf for the new type – Creates predicate for testing if something is of the new type

Constructor >(make-starship) #S(STARSHIP NAME NIL SPEED 0 CONDITION GREEN SHIELDS DOWN) Exact representation depends on implementation Use keywords to override default values of fields >(make-starship :name "Enterprise" :speed 12 :condition 'red-alert :shields 'damaged) #S(STARSHIP NAME "Enterprise" SPEED 12 CONDITION RED-ALERT SHIELDS DAMAGED)

Type Predicate >(defvar x (make-starship)) X >(starship-p x) T >(type-of x) STARSHIP >(type-of 'foo) SYMBOL >(starship-p 'foo) NIL

Accessors There is an accessor defined for each field in the defstruct >(starship-name x) NIL >(starship-speed x) 0 >(starship-condition x) GREEN >(starship-shields x) DOWN

Modifying Use accessor functions together with setf to modify fields >(setf (starship-name x) "Red Dwarf") "Red Dwarf" >x #S(STARSHIP NAME "Red Dwarf" SPEED 0 CONDITION GREEN SHIELDS DOWN)

Redefining Structures LISP will let you redefine structures with additional defstruct calls, but instances made under the old structure definition may no longer work correctly. It’s best to define a structure only once per session.

Describe takes any kind of LISP object and provides an informative description of it (implementation dependent) >(describe 'foo) FOO - internal symbol in USER package >(describe 7) 7 - fixnum (32 bits) >(describe x) Structure of type STARSHIP Byte:[Slot Type]Slot Name :Slot Value 0:NAME :"Red Dwarf" 4:SPEED :0 8:CONDITION :GREEN 12:SHIELDS :DOWN

Defstruct Options defstruct takes some further options – (:conc-name symbol) specifies the prefix of all accessor function names >(defstruct (square (:conc-name sq-)) (side 0)) SQUARE >(sq-side (make-square)) 0 – (:constructor symbol ) specifies a constructor name and optional order of arguments for constructor >(defstruct (rectangle (:constructor rect (length width))) (length 0) (width 0)) RECTANGLE >(rect 4 5) #S(RECTANGLE LENGTH 4 WIDTH 5) – (:predicate symbol) specifies the name of the predicate that tests to see if an item is of the type of the structure >(defstruct (starship (:predicate ssp)) (name nil) (speed 0)) STARSHIP >(ssp (make-starship)) T

Inheritance Structures can also inherit fields: so one structure can be defined as an extension of another Done with (:include parent) >(defstruct person (name nil) (age 0)) PERSON >(defstruct (student (:include person)) (major "undecided")) STUDENT >(make-student) #S(STUDENT NAME NIL AGE 0 MAJOR "undecided")

Symbol Property Lists Each symbol can have both a value and a function definition (this is why you can have variables named list or length in your functions) Another way to store several fields in a variable is through a symbol’s property list, which is the third thing every symbol possesses Property lists provide key/value info Not association lists, but lists of alternating keys and values

Get To get the value for a key in a symbol’s property list, use get (get symbol key) Update/add properties by using setf with get >(get 'car 'color) NIL >(setf (get 'car 'color) 'red) COLOR >(get 'car 'color) RED

Remprop remprop is used to remove a property Returns T if successful, NIL otherwise >(remprop 'car 'color) T >(get 'car 'color) NIL

Symbol-Plist symbol-plist lets you look at the entire property list >(setf (get 'dog 'color) 'black) COLOR >(setf (get 'dog 'breed) 'scotty) BREED >(symbol-plist 'dog) (BREED SCOTTY COLOR BLACK)