30
Summer 2003 Programming Languages Ch. 9 Ch. 9 1 jcmt CSE3302 CSE3302 Programming Languages Programming Languages (n-n-n-notes) (n-n-n-notes) Summer 2003 Dr. Carter Tiernan

CSE3302 Programming Languages (n-n-n-notes)

  • Upload
    edana

  • View
    39

  • Download
    0

Embed Size (px)

DESCRIPTION

CSE3302 Programming Languages (n-n-n-notes). Summer 2003 Dr. Carter Tiernan. Functional Programming. List Processing - LISP Complex interrelationships among data Recursion in conjunction with conditional expressions Primitive list-handling subroutines Applicative language. LISP. - PowerPoint PPT Presentation

Citation preview

Page 1: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 1 jcmt

CSE3302CSE3302Programming LanguagesProgramming Languages

(n-n-n-notes)(n-n-n-notes)Summer 2003

Dr. Carter Tiernan

Page 2: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 2 jcmt

Functional ProgrammingFunctional Programming• List Processing - LISP• Complex interrelationships among

data• Recursion in conjunction with

conditional expressions• Primitive list-handling subroutines• Applicative language

Page 3: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 3 jcmt

LISPLISP• Function applications

– Prefix (Polish) notation : flexibility– Fully parenthesized : no precedence

rules• Symbolic data

– Lists of symbols called atoms– List is ONLY data structure in LISP

Page 4: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 4 jcmt

ListsLists• S-expressions

– Function applications are evaluated– Quoted lists are treated as data

• Programs and data represented the same way– Convenient for one program to generate

and call for execution of another– Simple to write program manipulators

Page 5: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 5 jcmt

LISP ExecutionLISP Execution• Usually interpreted and often

interactive• Functions

– Pure : compute a value only• eq, plus, difference, etc.

– Pseudo : have side effects on computer state• set, defun, etc.

Page 6: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 6 jcmt

Data StructuresData Structures• Constructor : list• Primitives : atoms

– Numeric (integer and floating point)• Many ops provided: arithmetic, increment,

decrement, max, min, relational, predicates

– Nonnumeric character strings• Limited ops: comparisons for equality•nil (false, empty set) • null is the test op

Page 7: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 7 jcmt

ConstructorConstructor• List

– Surrounded by parentheses– Separated by blanks– Zero, one or more elements– Can contain lists– () equivalent to nil; called empty or

null list

Page 8: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 8 jcmt

Accessing Parts of a ListAccessing Parts of a List• Car

– Selects the first element of a list• Cdr

– Returns all of a list except its first element• Car and Cdr are selectors• Car and Cdr can be combined to

access interior parts of a list: ex. caadr

Page 9: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 9 jcmt

Building a listBuilding a list• Cons

– Creates a list from a first element and a list

– Is the inverse of car and cdr– Example:

• (car ‘(to be or not)) = to• (cdr ‘(to be or not)) = (be or not)• (cons ‘to ‘(be or not)) = (to be or not)

Page 10: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 10 jcmt

Info RepresentationInfo Representation• Property lists

– (p1 v1 p2 v2 … pn vn) where pn is the nth property and vn is the value of that property

– P-lists give flexibility– Easy to define a function to get

properties

Page 11: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 11 jcmt

Info RepresentationInfo Representation• Association lists

– ((a1 v1) (a2 v2) … (an vn))– Handles properties which are flags or

properties with multiple values

Page 12: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 12 jcmt

Recursive list constructionRecursive list construction• Append two lists together

– Identify simplest cases• (append ‘() L) = L• (append L ‘()) = L

– Reduce other cases to the simplest cases• (append L M) = (cons (car L) (append (cdr L) M)

– (defun append (L M) (if (null L))

M(cons (car L) (append (cdr L) M) ))

Page 13: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 13 jcmt

AtomsAtoms• Created by mentioning them• Have properties and relations (p-list)

– Print name / pname– putprop adds a property to an atom– apval denotes the binding of an atom to

a value – set binds an atom– symbol-plist shows property list of an

atom

Page 14: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 14 jcmt

List representationList representation• Linked list• Cells in the list have a right and a left

– Right part points to next cell in list– Left part points to value of the cell

• Data and programs both represented the same way– Program linked list called an expression

tree

Page 15: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 15 jcmt

List PrimitivesList Primitives• Efficient

– Car returns a left part– Cdr returns a right part– Cons requires storage allocation for new cell– Car, Cdr, and Cons do not change values

• Sublists can be shared• Pseudo-functions alter lists

– rplaca, rplacd – Have same drawbacks as aliasing

Page 16: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 16 jcmt

Conditional expressionConditional expression• Cond

– Mimics mathematical notation– Logical ops are defined in terms of

the conditional• And & Or

– Operands are evaluated sequentially– Conditional interpretation vs. strict

Page 17: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 17 jcmt

LISP on omegaLISP on omega• LISP interpreter on both omega

and gamma• Invoke by typing ‘lisp’• Exit by typing ‘(quit)’

• CMU Common Lisp• Info at www.cons.org/cmucl/

Page 18: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 18 jcmt

IterationIteration• Performed by recursion• Reduction

– Perform some op on every element of a list– Uses a binary function to reduce a list to a

single value• Mapping

– Apply a function to every element of a list– Returns a list of the results

• Filtering– Forms a sublist containing all elements that

satisfy some property

Page 19: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 19 jcmt

Functional argumentsFunctional arguments• Abstracting out pattern• Mapcar• Filter• Reduce• Suppress details • Simplify combination

Page 20: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 20 jcmt

Recursive InterpretersRecursive Interpreters• Arranged by cases

– Atoms• Numeric• Nonnumeric

– Quote– Conditional– Functions

Page 21: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 21 jcmt

InterpretersInterpreters• Primitive ops performed explicitly• User-defined functions

– Evaluate parameters– Bind formals to actuals– Add bindings to environment– Evaluate function in environment

Page 22: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 22 jcmt

Environment of evaluationEnvironment of evaluation• Arguments going to (apply f x a)

(p.380)– f the function - lambda expression– x the parameters - bound formals– a the environment - existing

environment with the addition of the bound formals

• Universal function

Page 23: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 23 jcmt

Static scopingStatic scoping• Closure to indicate environment of

definition - function keyword– Instruction part (ip) - program, lambda expr.– Environment part (ep) - definition

environment• (closure ip ep)• User-defined functions can be

– Dynamically scoped lambda expressions– Statically scoped function closures

Page 24: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 24 jcmt

Incompatible scope rulesIncompatible scope rules• Options

– Adopt uniform static scoping– Use default static scoping but allow

“special” dynamic scoping

Page 25: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 25 jcmt

Storage reclamationStorage reclamation• Explicit erasure

– Work for programmers– Security problems (dangling pointers)

• Automatic reclamation– Reference counts– Garbage collection

Page 26: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 26 jcmt

Reference countsReference counts• Accessible cells are referenced• Reference count keeps track of

how many other cells reference the current one

• Count must be incremented and decremented correctly

• Cells with zero count are added to the free list

Page 27: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 27 jcmt

Reference countsReference counts• Cycles defeat reference counts

– Cyclic reference boosts the count of each member plus one member has an outside reference.

– If outside reference goes away, cycle still has references internally but no access from anywhere else in program

– Allowing cycles is an open issue

Page 28: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 28 jcmt

Garbage CollectionGarbage Collection• When storage becomes low,

system starts up garbage collection• Mark and sweep process

– Starting at roots mark all cells that can be reached recursively moving from each cell to its children

– When all are marked, sweep through memory from top, free unmarked cells and reset flags on marked cells

Page 29: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 29 jcmt

Garbage CollectionGarbage Collection• Wreaks havoc on execution time• Non uniform and unpredictable• Approaches to more efficiency

include– Continuous garbage collection– Parallel

Page 30: CSE3302 Programming Languages (n-n-n-notes)

Summer 2003 Programming Languages Ch. 9Ch. 9 30 jcmt

LISPLISP• AI - represent and manipulate complex

interrelationships among symbolic data• Suited to ill-specified problems• Easy to manipulate LISP programs in

LISP