Chapter 14. Lisp, a functional programming language, is the second oldest language (next to...

71
Functional Programming Chapter 14

Transcript of Chapter 14. Lisp, a functional programming language, is the second oldest language (next to...

Page 1: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Functional Programming

Chapter 14

Page 2: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Lisp, a functional programming language, is the second oldest language (next to Fortran).

Motivated by a need to do symbolic, rather than numerical, manipulations.

Common LISP and Scheme are the two most popular dialects of the traditional LISP.

Haskell is a modern functional language that adds strong typing to the more usual features.

Other new functional languages: Erlang, Scala, Ocaml, even recent versions of Java,…

History of Functional Languages

Page 3: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

The name comes from Scalable Language Functional programming + OO concepts. Runs on the Java virtual machine Created by Martin Odersky Basis for many well-known systems today

including Twitter, FourSquare, and LinkedIn Coursera has offered free online courses in

Scala

Scala - http://www.scala-lang.org/

Page 4: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

LISt Processing The linked list is the major data structure Both programs and data can be represented

this way◦ Programs are able to manipulate code just like

any other data◦ Learning programs, …

History/Characteristics

Page 5: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

“The Lisp language is primarily for symbolic data processing. It has been used for symbolic calculations in differential and integral calculus, electrical circuit design, mathematic logic, game playing, and other fields of artificial intelligence.”, 1965, from the LISP 1.5 Programmer’s Manual

Statement of Purpose

Page 6: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

A function f from a set X (the domain) to a set Y (the range) maps each element x in X to a unique element y in Y.

For example, f(x) = x2 maps the set of real numbers into the set of positive real numbers.◦ i.e., the domain X is the set of all real numbers,

the range is the set of positive reals.

Background: Functions

Page 7: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

If f is a function from X to Y and g is a function from Y to Z, then (g ◦ f ) (x) is a function from X to Z defined as

(g ◦ f ) (x) = g(f (x)), for all x in XSimply put, it means to replace the x in g(x) with f (x).

Example:◦ f (x) = x2 + x◦ g(x) = 2x + 1◦ g ◦ f = 2*f(x) + 1 = 2(x2 + x ) + 1

Background: Functional Composition

Page 8: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Example using mathematical functions:◦ f (x) = x2 + x◦ g(x) = 2x + 1◦ g ◦ f = 2(x2 + x ) + 1

Example using programming language function◦int f (x) {x2 + x};◦int g(x) {2x + 1};◦Now, g(f(x))is {2 * f(x) + 1};

Background: Functional Composition

Page 9: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Functional programming mirrors mathematical functions: ◦ domain = input, range = output◦ A computation maps inputs to outputs

Variables are mathematical symbols; not associated with memory locations.◦ Compare to imperative programming where a variable

represents a value that is stored in memory and can be accessed by name as long as it is alive and in scope.

◦ In functional languages a symbol is used in a computation of a value that then can be used in additional computations.

Overview

Page 10: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Pure functional programming is state-free: no assignment statements.◦ In other words, no variables, no way to

permanently change memory. Programs in pure functional languages

consist of composite functions; output of each function becomes input to another.

Today, most functional languages have some imperative statements.

Overview

Page 11: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Typical mathematical function: Square(n) = n2

◦ funcName(args) = func-definition: an expression

Square : R R maps from reals to positive reals

A function is total if it is defined for all values of its domain. Otherwise, it is partial. e.g., Square is total.

14.1 Functions and the Lambda Calculus

Page 12: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

In imperative programming, variables are used to denote memory locations:

x = x + 1means “Update the program state by adding 1 to the value stored in the memory cell named x and storing the sum back in the memory cell.”◦ x is used two ways: as an r-value and an l-value

Semantics of Variables

Page 13: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Functional languages adopt the mathematical notion of a variable as something that represents an expression.◦ There is no association with a memory location or

modification of a variable’s value. So in functional languages a variable

represents an immutable value. ◦ Since variables don’t get updated, there’s no

concept of assignment and consequently no notion of program state.

Semantics of Variables

Page 14: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

The value of a function, in a functional language, depends only on the value of its arguments and not on any previous actions.

Contrast to the view of functions in imperative languages, where function values are based on arguments, order of evaluation, and can also have side effects (change state).◦ Since pure functional languages have no state

they also have no side effects.

Semantics of Variables

Page 15: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Referential transparency: a function’s result depends only upon the values of its arguments and not on any previous computation or the order of evaluation of its arguments.

However, most functional languages today are not “pure”; they have some version of the assignment statement.

Referential Transparency

Page 16: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

The lambda calculus, invented in 1941, is the foundation of functional programming

It “…provides simple semantics for computation with functions so that properties of functional computation can be studied.”http://www.answers.com/topic/lambda-calculus#Motivation

Lambda Calculus

Page 17: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Is a general model of computation◦ Can express anything that can be expressed by

a Turing machine; in other words, is Turing Complete

An imperative language works by changing the data store; a functional language works by applying functions and returning values◦ Lambda calculus models the latter approach◦ Some practitioners claim that it is harder to

make mistakes while using this style of programming.

Lambda Calculus

Page 18: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

In imperative and OO programming, functions have different (lower) status than variables.

In functional programming, functions have same status as variables; they are first-class entities.◦They can be passed as arguments in a call.

◦They can transform other functions.

Status of Functions

Page 19: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

A function that operates on other functions is called a functional form. e.g.,

g(f, [x1, x2, … ]) = [f(x1), f(x2), …], becomesg(Square, [2, 3, 5]) = [4, 9, 25]

Meaning: function g takes as parameters another function and a list (sequence) of values to which the parameter function is applied.

Example

Page 20: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Lists are written with white-space-separated elements enclosed in parentheses

Expressions are represented as lists Lists are treated as functions with the first

element being the operation and the remaining elements the arguments.

Programs as Lists

Page 21: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

A derivative of Lisp Our subset:

◦ omits assignments◦ simulates looping via recursion◦ simulates blocks via functional composition

Scheme is Turing complete, with recursion used to implement repetition.

14.2 Scheme

Page 22: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Cambridge prefix notation for all Scheme expressions: (f x1 x2 … xn)

e.g., (+ 2 2) ; evaluates to 4 (+(* 5 4 2)(-6 2));means 5*4*2+(6-2) Note: Scheme comments begin with ; Cambridge prefix allows operators to have

an arbitrary number of arguments.

14.2.1 Expressions

Page 23: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Used to define global “variables”; e.g.◦ (define f 120)◦ define is not equivalent to an assignment

statement. It just gives a name to a value as a convenient notation.

define can also be used for other purposes, as we will see.

setQ and setF are functions that operate more like assignments.

The define Function

Page 24: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Three steps:◦ replace names of symbols by their current

bindings.◦ Evaluate lists as function calls in Cambridge

prefix, where a list is a set of elements enclosed in ( ); e.g., (* f 2 ) The first element in the list is always treated as the

function unless you specifically say not to.◦ Constants evaluate to themselves.

14.2.2 Expression Evaluation

Page 25: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

5 ; evaluates to 5Given the global definition (define f 120) thenf ; evaluates to 120(+ f 5) ; evaluates to 125(+ 5 2 9 13) ; evaluates to 29#f ; predefined, false

5, #f, #t are constants, f is a bound symbol

Examples

Page 26: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

(+ 5 2 9 13) ; evaluates to 29but

(+ (5 2 9 13)); an errorScheme will try to evaluate the second list, interpreting “5” as a function

(f); error - f isn’t a function

Lists as Function Calls

Page 27: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

(define colors (quote (red yellow green)))or

(define colors (‘ (red yellow green)))

Quoting tells Scheme/LISP that the following list is not to be evaluated. This is a way of defining data.

Preventing Evaluation

Page 28: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

(define x f); defines x as 120 (value of f)

(define x ‘f); defines x as the symbol f

(define color ‘red) ; color is defined to be red

(define color red) ; error: no definition of red

Quoted Lists

Page 29: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

A list is a series of expressions enclosed in parentheses.◦ Lists represent both functions and data.◦ The empty list is written (). ◦ e.g., (0 2 4 6 8) is

a list of even numbers.

◦ Here’s how it’s stored:

14.2.3 Lists

Page 30: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Each list node is a record with two fields: the car and the cdr: car is (a pointer to) the first field, cdr is (a pointer to) the second.◦ Typically referred to as cons cells

Note that in the previous example the cdr of the last node ( |8|nil| ) is nil.◦ This is equivalent to the null pointer in C++

The nil value can be represented as ( ), which is also the representation for an empty list.

List Node Structure

Page 31: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Proper lists are assumed to end with the value( ) which is implemented by null reference

In a dotted list the last cons has some value other than nil as the value of the cdr field.◦ “Dotted” lists are written (0 2 4 6 . 8) The last node

in the previous list would have been |6|8|

“Proper” Lists & Dotted Lists

Page 32: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Structure of a List in SchemeFigure 14.1

(a)

(b)

Notice the difference between the list with nil as the last entry, and the “dotted” list shown in part b.

Page 33: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

a

nil

nilc

b

List Elements Can Be Lists Themselves

This represents the list (a (b c))

Page 34: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Because each node in a Scheme list consists of two pointers, internally the lists are similar to binary trees.

Recall: The links (pointers) are called the 'car' and the 'cdr'. ◦ A list node (cell) is also called a cons or a pair.

Lists as Binary Trees

Page 35: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

car: returns the first element (“head”) of a list

cdr: returns the tail of the list, which is itself a list

List Functions

Page 36: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Suppose we write (define evens ‘(0 2 4 6 8)). Then:(car evens) ; gives 0

(cdr evens) ; gives (2 4 6 8)

( (null? ‘()) ; gives #t, or true

(cdr(cdr evens)); (4 6 8)

(car‘(6 8)) ; 6 (quoted to stop eval)

List Transforming Functions

Page 37: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

cons: used to build lists◦ Requires two arguments: an element and a list; e.g., ◦ (cons 8 ( )) ; gives the 1-element list (8)◦ (cons 6 (cons 8( ))) ; gives the list (6 8)◦ (cons 6 ‘(8)) ; also gives the list (6 8)◦ (cons 4(cons 8 9)) ; gives the dotted list

; (4 8 . 9 ) since 9 is not a

; list

List Functions

Page 38: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

list takes an arbitrary number of arguments and creates a list; append takes two lists and concatenates them:◦(append ‘(1 3 5) evens)

;gives(1 3 5 0 2 4 6 8)◦(append evens (list 3)

; gives (0 2 4 6 8 3)◦(list ‘(1 3 5) evens)

; gives ((1 3 5) (0 2 4 6 8))

◦(list 1 2 3 4) ; gives (1 2 3 4)◦(list ‘evens ‘odds) ;gives (evens odds)

List Building Functions

Page 39: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

The function equal? returns true if the two objects have the same structure and content.

(equal? 5 ‘(5)) ; gives #f, or false

(equal? 5 5) ; #t(equal? ‘(1 2 3) ‘(1 (2 3)))

; returns false

Equal

Page 40: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Numbers: integers, rationals, and floating point numbers

Characters Functions Symbols Strings Boolean values #t and #f All values except #f and ( ) are interpreted

as true when used as a predicate.

14.2.4 – Elementary Values

Page 41: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

(if test then-part)(if test then-part else-part)

Example:

(if (< x 0) (- 0 x)) ;returns –x if x<0(if (< x y) x y)

; returns the smaller of x, y

Control Flow

Page 42: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

The case statement is similar to a Java or C++ switch statement:

(case month(( sep apr jun nov) 30)((feb) 28)(else 31) ; optional)

All cases take an unquoted list of constants, except for the else.

Control Flow

Page 43: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

define is also used to define functions; according to the following syntax:(define name (lambda (arguments)

body))or(define (name arguments) body)

From the former, based on Scheme as an example of an applied lambda calculus.

Defining Functions

Page 44: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

The lambda calculus isbased on the notion ofvariable bindings in functions.◦ The lambda is arbitrary – doesn’t

have any mnemonic significance Lambda calculus was invented

in 1945 as a way of studyinglogical formalisms & has relevance in computer science, especially programming language theory.

Defining Functions

Page 45: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

(define (min x y) (if (< x y) x y)) name args body

to return the minimum of x and y (define (abs x)(if(< x 0)(- 0 x) x))

to find the absolute value of x

Examples

Page 46: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

;; bulls-eye? : Number Number -> Boolean;; par1 par2 result;; determines if(x,y)is in the bull's eye (define (bulls-eye? x y) (<= (dist-to- origin x y) 5))

name args body

;; dist-to-origin: Number Number -> Boolean ;; determines distance from (0,0) to (x,y) (define (dist-to-origin x y) (sqrt (+(* x x)

(* y y))))

name args body

Page 47: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

(define (factorial n) (if(< n 1) 1 (* n factorial(-n 1)))))

(define (fun1 alist)(if (null? alist) 0 (+ (car alist)(fun1(cdr

alist)))))◦Suppose ‘alist’ is replaced by ‘evens’

Recursive functions

Page 48: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

length: returns the number of elements in a list◦ (length ‘(1 2 3 4)) ; returns 4◦ (length ‘((1 2 3) (5 6 7) 8)) ; returns 3

(define (length alist)(if (null? alist) 0 (+ 1 (length (cdr

alist)))))

Built-in Functions

Page 49: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

member: tests whether an element is in a list(member 4 evens);returns (4 6 8) (member 1 ‘(4 2)); returns ()

If the element is in the list, return portion of the list starting with that element, else return the empty list.

Built-in Functions

Page 50: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

The mapcar function takes two parameters: a function fun and a list alist.

mapcar applies the function to the list of values, one at a time, and returns a list of the results.

Functions as Parameters

Page 51: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

(define (mapcar fun alist)(if (null? alist) ‘( )

(cons (fun (car alist))(mapcar fun (cdr

alist)))))

E.g., if (define (square x) (* x x)) then(mapcar square ‘(2 3 5 7

9)) returns(4 9 25 49 81)

Functions as Parameters

Page 52: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

14.2.9 Symbolic Differentiation

• Symbolic Differentiation Rules• Fig 14.2

d

dx(c) 0 c is a constant

d

dx(x) 1

d

dx(u v) du

dx dvdx

u and v are functions of x

d

dx(u v)

du

dxdv

dxd

dx(uv) u dv

dx v dudx

d

dx(u /v) v

du

dx udv

dx

/v 2

Page 53: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

(define (diff x expr)(if (not (list? expr))

(if (equal? x expr) 1 0)(let ((u (cadr expr)) (v (caddr expr))) (case (car expr) ((+) (list ‘+ (diff x u) (diff x v))) ((-) (list ‘- (diff x u) (diff x v))) ((*) (list ‘+ (list ‘* u (diff x v))

(list ‘* v (diff x u)))) ((/) (list ‘div (list ‘- (list ‘* v (diff x u))

(list ‘* u (diff x v))) (list ‘* u v)))

))))

Page 54: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Usually implemented as an interpreted language, although there are compiled forms.

A LISP interpreter generally consists of:◦ a memory manager◦ collection of core functions implemented in

compiled form for speed◦ a set of support functions implemented with the

core

Implementation of LISPand its Dialects

Page 55: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Unlike most other languages, no distinction is made between "expressions" and "statements"

Code and data are written as expressions. When an expression is evaluated, it

produces a value (or list of values), which then can be embedded into other expressions.

Lisp is an Expression-Oriented Language.

Page 56: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

In Scheme, processing proceeds by typing expressions into the Scheme environment. After each expression is entered, Scheme ◦ reads the expression, ◦ evaluates the expression to determine its value,

and ◦ prints the resulting value. ◦ This read-eval-print cycle forms the basis for all

processing within Scheme.

Using Scheme

Page 57: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Memory is organized into cells (or nodes).

all cells are the same size, usually 2 words of memory

cells form a large pool of available storage, chained together into the initial free list.

cells are taken off the free list as needed to build atoms, lists, etc.

Memory Management in LISP

Page 58: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Literal atoms may require several cells to hold their definition (Remember the list diagram)

Each cell must be big enough to hold a pointer to the next list element, and a pointer to the current element value

Memory Management in Lisp

Page 59: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Evaluations in LISP take needed cells off of the free list. Many results are temporary and then are of no more use.

Therefore, must have mechanism for reclaiming space.

Lisp was the first language to use automatic garbage collection extensively

Garbage Collection

Page 60: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Another Lisp Quote

– Lisp has jokingly been called "the most intelligent way to misuse a computer". I think that description is a great compliment because it transmits the full flavor of liberation: it has assisted a number of our most gifted fellow humans in thinking previously impossible thoughts.

– — Edsger Dijkstra, CACM, 15:10

Page 61: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Other Programming Paradigms

Event-DrivenConcurrent

Page 62: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Event-drive programs do not control the sequence in which input events occur; instead, they are written to react to any reasonable sequence of events.

Compare to imperative, OO, functional, and logical paradigms in which the ordering of input influences the way program steps are executed

Event-Driven P.L. Paradigm

Page 63: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Graphical User Interface (GUI) programs Online registration systems Airline reservation systems Embedded applications for devices such as

cell phones, cars, airplane navigation systems, robots, home security systems …

Examples

Page 64: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Imperative programs get input, operate on it and produce results, either in a loop or in a single pass. Programmer determines order of input.

Event-driven programs maintain an event queue that corresponds to events that occur asynchronously and are processed as they happen.◦ Event-driven programs tend to run

continuously, waiting for input

Event-driven control

Page 65: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Provides certain classes and methods that can be used to design an interaction◦ Specify events, associate an event with an object,

and provide event-handling actions Figure 16.3 shows a partial class hierarchy

with subclasses such as button, popup menu, text area, etc.

Listeners recognize an event, such as a mouse click.

Java Event-Processing Features

Page 66: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Concurrency occurs when two or more separate threads or programs execute at the “same” time – actually, or apparently.

Example: several objects of the same class (e.g basketBallPlayer) may model a real-world situation in which several actual players interact concurrently

Example: a web browser downloads a page from a web site; several threads cooperate by rendering different parts of the page

Concurrent Programming

Page 67: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Concurrency is implemented in one of two ways: Concurrent program units execute◦ on separate processors at the same time, or◦ on a single processor by implementing time

slicing: switching back and forth rapidly between several separate programs (processes, threads)

History of Concurrency

Page 68: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Multiprogramming is an early operating system technique that time-shares CPU cycles between several different processes, all loaded in memory.◦ Originally introduced to give the CPU something

to do when a process was waiting for input◦ Later extended by interrupting a running process

to give another process a chance

History of Concurrency

Page 69: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Concurrency was first implemented at the operating system level. Later techniques extended it to application level programs.

Concurrency at the application level requires some kind of synchronization to prevent possible data corruption◦ Semaphores◦ Monitors – Java threads◦ Dedicated concurrent languages: concurrent

Pascal, concurrent Haskell, Cilk (concurrent C), ….

History of Concurrency

Page 70: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

A concurrent program is a program designed to have two or more execution contexts. Such a program is said to be multithreaded.◦ A parallel program is a concurrent program in which

several threads are active simultaneously (implies multiple processors, but conceptually the same thing)

A distributed program is a concurrent program that is designed to executed simultaneously on a collection of processors that don’t share memory.◦ Not the same – cannot share data directly, aren’t

generally considered to be ‘multi-threaded’

Author Definitions

Page 71: Chapter 14.  Lisp, a functional programming language, is the second oldest language (next to Fortran).  Motivated by a need to do symbolic, rather than.

Race conditions / synchronization Deadlock Interprocess communication

Concurrency Issues