cs345h: programming languages lecture: lisptdillig/cs345h/lecture19.pdfthomas dillig, cs345h:...

153
CS345H: Programming Languages Lecture: LISP Thomas Dillig Thomas Dillig, CS345H: Programming Languages Lecture: LISP 1/30

Upload: others

Post on 29-Jan-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

  • CS345H: Programming Languages

    Lecture: LISP

    Thomas Dillig

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 1/30

  • Historical Perspective

    I Second oldest programming language after Fortran

    I Developed in late 1950’s at MIT by John McCarthy

    I Original purpose: to facilitate research in artificial intelligence

    I In research, one may not initially know solution to problem;thus, development of program and research hand-in-hand

    I Thus, program may need to be changed radically as a result ofexperimental evaluation: exploratory programming

    I LISP is popular for exploratory programming

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 2/30

  • Historical Perspective

    I Second oldest programming language after Fortran

    I Developed in late 1950’s at MIT by John McCarthy

    I Original purpose: to facilitate research in artificial intelligence

    I In research, one may not initially know solution to problem;thus, development of program and research hand-in-hand

    I Thus, program may need to be changed radically as a result ofexperimental evaluation: exploratory programming

    I LISP is popular for exploratory programming

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 2/30

  • Historical Perspective

    I Second oldest programming language after Fortran

    I Developed in late 1950’s at MIT by John McCarthy

    I Original purpose: to facilitate research in artificial intelligence

    I In research, one may not initially know solution to problem;thus, development of program and research hand-in-hand

    I Thus, program may need to be changed radically as a result ofexperimental evaluation: exploratory programming

    I LISP is popular for exploratory programming

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 2/30

  • Historical Perspective

    I Second oldest programming language after Fortran

    I Developed in late 1950’s at MIT by John McCarthy

    I Original purpose: to facilitate research in artificial intelligence

    I In research, one may not initially know solution to problem;thus, development of program and research hand-in-hand

    I Thus, program may need to be changed radically as a result ofexperimental evaluation: exploratory programming

    I LISP is popular for exploratory programming

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 2/30

  • Historical Perspective

    I Second oldest programming language after Fortran

    I Developed in late 1950’s at MIT by John McCarthy

    I Original purpose: to facilitate research in artificial intelligence

    I In research, one may not initially know solution to problem;thus, development of program and research hand-in-hand

    I Thus, program may need to be changed radically as a result ofexperimental evaluation: exploratory programming

    I LISP is popular for exploratory programming

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 2/30

  • Historical Perspective

    I Second oldest programming language after Fortran

    I Developed in late 1950’s at MIT by John McCarthy

    I Original purpose: to facilitate research in artificial intelligence

    I In research, one may not initially know solution to problem;thus, development of program and research hand-in-hand

    I Thus, program may need to be changed radically as a result ofexperimental evaluation: exploratory programming

    I LISP is popular for exploratory programming

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 2/30

  • LISP, Lists, and Dialects

    I The main data structures in LISP are lists

    I LISP is named after its main data structure: LISt Processor

    I Another feature of LISP: need to write lots of parentheses!

    I Students who hate parentheses in LISP have given it manynicknames:

    I ”Lost In Stupid Parentheses”

    I ”Lots of Irritating Superfluous Parentheses”

    I Today, there are many variations of the original LISP:Common LISP, Scheme, Clojure

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 3/30

  • LISP, Lists, and Dialects

    I The main data structures in LISP are lists

    I LISP is named after its main data structure: LISt Processor

    I Another feature of LISP: need to write lots of parentheses!

    I Students who hate parentheses in LISP have given it manynicknames:

    I ”Lost In Stupid Parentheses”

    I ”Lots of Irritating Superfluous Parentheses”

    I Today, there are many variations of the original LISP:Common LISP, Scheme, Clojure

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 3/30

  • LISP, Lists, and Dialects

    I The main data structures in LISP are lists

    I LISP is named after its main data structure: LISt Processor

    I Another feature of LISP: need to write lots of parentheses!

    I Students who hate parentheses in LISP have given it manynicknames:

    I ”Lost In Stupid Parentheses”

    I ”Lots of Irritating Superfluous Parentheses”

    I Today, there are many variations of the original LISP:Common LISP, Scheme, Clojure

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 3/30

  • LISP, Lists, and Dialects

    I The main data structures in LISP are lists

    I LISP is named after its main data structure: LISt Processor

    I Another feature of LISP: need to write lots of parentheses!

    I Students who hate parentheses in LISP have given it manynicknames:

    I ”Lost In Stupid Parentheses”

    I ”Lots of Irritating Superfluous Parentheses”

    I Today, there are many variations of the original LISP:Common LISP, Scheme, Clojure

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 3/30

  • LISP, Lists, and Dialects

    I The main data structures in LISP are lists

    I LISP is named after its main data structure: LISt Processor

    I Another feature of LISP: need to write lots of parentheses!

    I Students who hate parentheses in LISP have given it manynicknames:

    I ”Lost In Stupid Parentheses”

    I ”Lots of Irritating Superfluous Parentheses”

    I Today, there are many variations of the original LISP:Common LISP, Scheme, Clojure

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 3/30

  • LISP, Lists, and Dialects

    I The main data structures in LISP are lists

    I LISP is named after its main data structure: LISt Processor

    I Another feature of LISP: need to write lots of parentheses!

    I Students who hate parentheses in LISP have given it manynicknames:

    I ”Lost In Stupid Parentheses”

    I ”Lots of Irritating Superfluous Parentheses”

    I Today, there are many variations of the original LISP:Common LISP, Scheme, Clojure

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 3/30

  • LISP, Lists, and Dialects

    I The main data structures in LISP are lists

    I LISP is named after its main data structure: LISt Processor

    I Another feature of LISP: need to write lots of parentheses!

    I Students who hate parentheses in LISP have given it manynicknames:

    I ”Lost In Stupid Parentheses”

    I ”Lots of Irritating Superfluous Parentheses”

    I Today, there are many variations of the original LISP:Common LISP, Scheme, Clojure

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 3/30

  • Overview Of Important Ideas in LISP

    I Functional, expression-based language

    I Heavily influenced by lambda calculus

    I First language to allow recursive and higher-order functions

    I Lists are fundamental data type

    I ”Programs as data:” programs and data represented same way

    I Dynamic scoping (in original LISP)

    I First language that has garbage collection

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 4/30

  • Overview Of Important Ideas in LISP

    I Functional, expression-based language

    I Heavily influenced by lambda calculus

    I First language to allow recursive and higher-order functions

    I Lists are fundamental data type

    I ”Programs as data:” programs and data represented same way

    I Dynamic scoping (in original LISP)

    I First language that has garbage collection

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 4/30

  • Overview Of Important Ideas in LISP

    I Functional, expression-based language

    I Heavily influenced by lambda calculus

    I First language to allow recursive and higher-order functions

    I Lists are fundamental data type

    I ”Programs as data:” programs and data represented same way

    I Dynamic scoping (in original LISP)

    I First language that has garbage collection

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 4/30

  • Overview Of Important Ideas in LISP

    I Functional, expression-based language

    I Heavily influenced by lambda calculus

    I First language to allow recursive and higher-order functions

    I Lists are fundamental data type

    I ”Programs as data:” programs and data represented same way

    I Dynamic scoping (in original LISP)

    I First language that has garbage collection

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 4/30

  • Overview Of Important Ideas in LISP

    I Functional, expression-based language

    I Heavily influenced by lambda calculus

    I First language to allow recursive and higher-order functions

    I Lists are fundamental data type

    I ”Programs as data:” programs and data represented same way

    I Dynamic scoping (in original LISP)

    I First language that has garbage collection

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 4/30

  • Overview Of Important Ideas in LISP

    I Functional, expression-based language

    I Heavily influenced by lambda calculus

    I First language to allow recursive and higher-order functions

    I Lists are fundamental data type

    I ”Programs as data:” programs and data represented same way

    I Dynamic scoping (in original LISP)

    I First language that has garbage collection

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 4/30

  • Overview Of Important Ideas in LISP

    I Functional, expression-based language

    I Heavily influenced by lambda calculus

    I First language to allow recursive and higher-order functions

    I Lists are fundamental data type

    I ”Programs as data:” programs and data represented same way

    I Dynamic scoping (in original LISP)

    I First language that has garbage collection

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 4/30

  • LISP as a Functional Language

    I LISP is a functional programming language

    I Characteristic of functional languages

    1. No state or mutable data

    2. No statements (such as assignment), only expressions!

    3. Expressions have no side-effects

    I You are already very familiar with a functional language: L!

    I All functional languages have their roots in lambda calculus

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 5/30

  • LISP as a Functional Language

    I LISP is a functional programming language

    I Characteristic of functional languages

    1. No state or mutable data

    2. No statements (such as assignment), only expressions!

    3. Expressions have no side-effects

    I You are already very familiar with a functional language: L!

    I All functional languages have their roots in lambda calculus

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 5/30

  • LISP as a Functional Language

    I LISP is a functional programming language

    I Characteristic of functional languages

    1. No state or mutable data

    2. No statements (such as assignment), only expressions!

    3. Expressions have no side-effects

    I You are already very familiar with a functional language: L!

    I All functional languages have their roots in lambda calculus

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 5/30

  • LISP as a Functional Language

    I LISP is a functional programming language

    I Characteristic of functional languages

    1. No state or mutable data

    2. No statements (such as assignment), only expressions!

    3. Expressions have no side-effects

    I You are already very familiar with a functional language: L!

    I All functional languages have their roots in lambda calculus

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 5/30

  • LISP as a Functional Language

    I LISP is a functional programming language

    I Characteristic of functional languages

    1. No state or mutable data

    2. No statements (such as assignment), only expressions!

    3. Expressions have no side-effects

    I You are already very familiar with a functional language: L!

    I All functional languages have their roots in lambda calculus

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 5/30

  • LISP as a Functional Language

    I LISP is a functional programming language

    I Characteristic of functional languages

    1. No state or mutable data

    2. No statements (such as assignment), only expressions!

    3. Expressions have no side-effects

    I You are already very familiar with a functional language: L!

    I All functional languages have their roots in lambda calculus

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 5/30

  • LISP as a Functional Language

    I LISP is a functional programming language

    I Characteristic of functional languages

    1. No state or mutable data

    2. No statements (such as assignment), only expressions!

    3. Expressions have no side-effects

    I You are already very familiar with a functional language: L!

    I All functional languages have their roots in lambda calculus

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 5/30

  • LISP and Lambda Calculus

    I Just like L and λ-calculus, Lisp also has lambda expressions!

    I This is how you write a function that adds 1 to its argument:

    (lambda (x) (+ x 1))

    I In LISP, must write + x 1 instead of x+1 because alloperations are in prefix form (to make parsing easier!)

    I This is how you write application expressions in LISP:

    ((lambda (x) (+ x 1)) 3)

    I Evaluates to 4

    I Don’t forget any of the parentheses – otherwise, won’t work!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 6/30

  • LISP and Lambda Calculus

    I Just like L and λ-calculus, Lisp also has lambda expressions!

    I This is how you write a function that adds 1 to its argument:

    (lambda (x) (+ x 1))

    I In LISP, must write + x 1 instead of x+1 because alloperations are in prefix form (to make parsing easier!)

    I This is how you write application expressions in LISP:

    ((lambda (x) (+ x 1)) 3)

    I Evaluates to 4

    I Don’t forget any of the parentheses – otherwise, won’t work!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 6/30

  • LISP and Lambda Calculus

    I Just like L and λ-calculus, Lisp also has lambda expressions!

    I This is how you write a function that adds 1 to its argument:

    (lambda (x) (+ x 1))

    I In LISP, must write + x 1 instead of x+1 because alloperations are in prefix form (to make parsing easier!)

    I This is how you write application expressions in LISP:

    ((lambda (x) (+ x 1)) 3)

    I Evaluates to 4

    I Don’t forget any of the parentheses – otherwise, won’t work!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 6/30

  • LISP and Lambda Calculus

    I Just like L and λ-calculus, Lisp also has lambda expressions!

    I This is how you write a function that adds 1 to its argument:

    (lambda (x) (+ x 1))

    I In LISP, must write + x 1 instead of x+1 because alloperations are in prefix form (to make parsing easier!)

    I This is how you write application expressions in LISP:

    ((lambda (x) (+ x 1)) 3)

    I Evaluates to 4

    I Don’t forget any of the parentheses – otherwise, won’t work!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 6/30

  • LISP and Lambda Calculus

    I Just like L and λ-calculus, Lisp also has lambda expressions!

    I This is how you write a function that adds 1 to its argument:

    (lambda (x) (+ x 1))

    I In LISP, must write + x 1 instead of x+1 because alloperations are in prefix form (to make parsing easier!)

    I This is how you write application expressions in LISP:

    ((lambda (x) (+ x 1)) 3)

    I Evaluates to 4

    I Don’t forget any of the parentheses – otherwise, won’t work!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 6/30

  • LISP and Lambda Calculus

    I Just like L and λ-calculus, Lisp also has lambda expressions!

    I This is how you write a function that adds 1 to its argument:

    (lambda (x) (+ x 1))

    I In LISP, must write + x 1 instead of x+1 because alloperations are in prefix form (to make parsing easier!)

    I This is how you write application expressions in LISP:

    ((lambda (x) (+ x 1)) 3)

    I Evaluates to 4

    I Don’t forget any of the parentheses – otherwise, won’t work!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 6/30

  • Higher-Order Functions

    I Just like L and λ-calculus, very easy to write higher-orderfunctions in LISP

    I A higher-order function is one that takes other functions asinput or returns a function

    I Here is higher-order function in LISP for composing twofunctions:

    (lambda (f g) ( (lambda (x) (f(g x)))))

    I This function takes as input two function f and g and anotherargument x and computes f(g(x))

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 7/30

  • Higher-Order Functions

    I Just like L and λ-calculus, very easy to write higher-orderfunctions in LISP

    I A higher-order function is one that takes other functions asinput or returns a function

    I Here is higher-order function in LISP for composing twofunctions:

    (lambda (f g) ( (lambda (x) (f(g x)))))

    I This function takes as input two function f and g and anotherargument x and computes f(g(x))

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 7/30

  • Higher-Order Functions

    I Just like L and λ-calculus, very easy to write higher-orderfunctions in LISP

    I A higher-order function is one that takes other functions asinput or returns a function

    I Here is higher-order function in LISP for composing twofunctions:

    (lambda (f g) ( (lambda (x) (f(g x)))))

    I This function takes as input two function f and g and anotherargument x and computes f(g(x))

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 7/30

  • Higher-Order Functions

    I Just like L and λ-calculus, very easy to write higher-orderfunctions in LISP

    I A higher-order function is one that takes other functions asinput or returns a function

    I Here is higher-order function in LISP for composing twofunctions:

    (lambda (f g) ( (lambda (x) (f(g x)))))

    I This function takes as input two function f and g and anotherargument x and computes f(g(x))

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 7/30

  • Named vs. Anonymous Functions

    I Lambda expressions such as (lambda (x) (+ x 1)) areanonymous functions (i.e., functions without a name)

    I Like L, LISP also allows giving functions a name

    I This is how you name functions in LISP:

    (define compose (lambda (f g) ((lambda (x) (f(g x)))))

    I You can call named functions like this:

    (compose (lambda (x) (+x x)) (lambda (x) (* x x)))

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 8/30

  • Named vs. Anonymous Functions

    I Lambda expressions such as (lambda (x) (+ x 1)) areanonymous functions (i.e., functions without a name)

    I Like L, LISP also allows giving functions a name

    I This is how you name functions in LISP:

    (define compose (lambda (f g) ((lambda (x) (f(g x)))))

    I You can call named functions like this:

    (compose (lambda (x) (+x x)) (lambda (x) (* x x)))

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 8/30

  • Named vs. Anonymous Functions

    I Lambda expressions such as (lambda (x) (+ x 1)) areanonymous functions (i.e., functions without a name)

    I Like L, LISP also allows giving functions a name

    I This is how you name functions in LISP:

    (define compose (lambda (f g) ((lambda (x) (f(g x)))))

    I You can call named functions like this:

    (compose (lambda (x) (+x x)) (lambda (x) (* x x)))

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 8/30

  • Named vs. Anonymous Functions

    I Lambda expressions such as (lambda (x) (+ x 1)) areanonymous functions (i.e., functions without a name)

    I Like L, LISP also allows giving functions a name

    I This is how you name functions in LISP:

    (define compose (lambda (f g) ((lambda (x) (f(g x)))))

    I You can call named functions like this:

    (compose (lambda (x) (+x x)) (lambda (x) (* x x)))

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 8/30

  • Named Functions and Recursion

    I As we saw in L, named functions make recursion more natural

    I Here is a recursive function in LISP:

    (define f (lambda (x)

    (cond ((eq x 0) 0) (true + x f(- x 1))))

    I This function uses a conditional expression:

    (cond (p1 e1) ... (pn en))

    I Here, we evaluate each condition p in order from left-to-right

    I If pi is the first expression that evaluates to true, then weevaluate corresponding expression ei

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 9/30

  • Named Functions and Recursion

    I As we saw in L, named functions make recursion more natural

    I Here is a recursive function in LISP:

    (define f (lambda (x)

    (cond ((eq x 0) 0) (true + x f(- x 1))))

    I This function uses a conditional expression:

    (cond (p1 e1) ... (pn en))

    I Here, we evaluate each condition p in order from left-to-right

    I If pi is the first expression that evaluates to true, then weevaluate corresponding expression ei

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 9/30

  • Named Functions and Recursion

    I As we saw in L, named functions make recursion more natural

    I Here is a recursive function in LISP:

    (define f (lambda (x)

    (cond ((eq x 0) 0) (true + x f(- x 1))))

    I This function uses a conditional expression:

    (cond (p1 e1) ... (pn en))

    I Here, we evaluate each condition p in order from left-to-right

    I If pi is the first expression that evaluates to true, then weevaluate corresponding expression ei

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 9/30

  • Named Functions and Recursion

    I As we saw in L, named functions make recursion more natural

    I Here is a recursive function in LISP:

    (define f (lambda (x)

    (cond ((eq x 0) 0) (true + x f(- x 1))))

    I This function uses a conditional expression:

    (cond (p1 e1) ... (pn en))

    I Here, we evaluate each condition p in order from left-to-right

    I If pi is the first expression that evaluates to true, then weevaluate corresponding expression ei

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 9/30

  • Named Functions and Recursion

    I As we saw in L, named functions make recursion more natural

    I Here is a recursive function in LISP:

    (define f (lambda (x)

    (cond ((eq x 0) 0) (true + x f(- x 1))))

    I This function uses a conditional expression:

    (cond (p1 e1) ... (pn en))

    I Here, we evaluate each condition p in order from left-to-right

    I If pi is the first expression that evaluates to true, then weevaluate corresponding expression ei

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 9/30

  • Named Functions and Recursion

    (define f (lambda (x)

    (cond ((eq x 0) 0) (true + x f(- x 1))))

    I The expression (eq e1 e2) evaluates to true if e1 and e2are equal, and to false otherwise

    I So, what does the recursive function above compute?

    I Recall: possible to encode recursion without named functions

    I McCarthy’s original paper on LISP remarks that lambdaexpressions are not sufficient for writing recursive functions

    I Apparently, Y-combinator not known to McCarthy and hisgroup back then – you know better!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 10/30

  • Named Functions and Recursion

    (define f (lambda (x)

    (cond ((eq x 0) 0) (true + x f(- x 1))))

    I The expression (eq e1 e2) evaluates to true if e1 and e2are equal, and to false otherwise

    I So, what does the recursive function above compute?

    I Recall: possible to encode recursion without named functions

    I McCarthy’s original paper on LISP remarks that lambdaexpressions are not sufficient for writing recursive functions

    I Apparently, Y-combinator not known to McCarthy and hisgroup back then – you know better!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 10/30

  • Named Functions and Recursion

    (define f (lambda (x)

    (cond ((eq x 0) 0) (true + x f(- x 1))))

    I The expression (eq e1 e2) evaluates to true if e1 and e2are equal, and to false otherwise

    I So, what does the recursive function above compute?

    I Recall: possible to encode recursion without named functions

    I McCarthy’s original paper on LISP remarks that lambdaexpressions are not sufficient for writing recursive functions

    I Apparently, Y-combinator not known to McCarthy and hisgroup back then – you know better!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 10/30

  • Named Functions and Recursion

    (define f (lambda (x)

    (cond ((eq x 0) 0) (true + x f(- x 1))))

    I The expression (eq e1 e2) evaluates to true if e1 and e2are equal, and to false otherwise

    I So, what does the recursive function above compute?

    I Recall: possible to encode recursion without named functions

    I McCarthy’s original paper on LISP remarks that lambdaexpressions are not sufficient for writing recursive functions

    I Apparently, Y-combinator not known to McCarthy and hisgroup back then – you know better!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 10/30

  • Named Functions and Recursion

    (define f (lambda (x)

    (cond ((eq x 0) 0) (true + x f(- x 1))))

    I The expression (eq e1 e2) evaluates to true if e1 and e2are equal, and to false otherwise

    I So, what does the recursive function above compute?

    I Recall: possible to encode recursion without named functions

    I McCarthy’s original paper on LISP remarks that lambdaexpressions are not sufficient for writing recursive functions

    I Apparently, Y-combinator not known to McCarthy and hisgroup back then – you know better!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 10/30

  • Atoms and Lists

    I Two fundamental data types in LISP: atoms and lists

    I Atoms are integers, floating point numbers and symbolicvalues, such as x

    I Lists in LISP are singly-linked lists; each cell in list called cons

    I A cons consists of two pointers, called car and cdr

    I Can think of car as data stored in current list cell and cdr isa pointer to the rest of the list

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 11/30

  • Atoms and Lists

    I Two fundamental data types in LISP: atoms and lists

    I Atoms are integers, floating point numbers and symbolicvalues, such as x

    I Lists in LISP are singly-linked lists; each cell in list called cons

    I A cons consists of two pointers, called car and cdr

    I Can think of car as data stored in current list cell and cdr isa pointer to the rest of the list

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 11/30

  • Atoms and Lists

    I Two fundamental data types in LISP: atoms and lists

    I Atoms are integers, floating point numbers and symbolicvalues, such as x

    I Lists in LISP are singly-linked lists; each cell in list called cons

    I A cons consists of two pointers, called car and cdr

    I Can think of car as data stored in current list cell and cdr isa pointer to the rest of the list

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 11/30

  • Atoms and Lists

    I Two fundamental data types in LISP: atoms and lists

    I Atoms are integers, floating point numbers and symbolicvalues, such as x

    I Lists in LISP are singly-linked lists; each cell in list called cons

    I A cons consists of two pointers, called car and cdr

    I Can think of car as data stored in current list cell and cdr isa pointer to the rest of the list

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 11/30

  • Atoms and Lists

    I Two fundamental data types in LISP: atoms and lists

    I Atoms are integers, floating point numbers and symbolicvalues, such as x

    I Lists in LISP are singly-linked lists; each cell in list called cons

    I A cons consists of two pointers, called car and cdr

    I Can think of car as data stored in current list cell and cdr isa pointer to the rest of the list

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 11/30

  • List Operators

    I List operations in LISP are: cons car cdr

    I cons appends two lists or atoms together

    I car,cdr take lists apart: car gives first elem,cdr gives rest

    I nil denotes empty list

    I Example: (cons 1 2 (cons 3 4)) evaluates to (1 2 (3 4))

    I Example:(cons 1 2 (cdr (cons 3 4))) evaluates to (1 2 3)

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 12/30

  • List Operators

    I List operations in LISP are: cons car cdr

    I cons appends two lists or atoms together

    I car,cdr take lists apart: car gives first elem,cdr gives rest

    I nil denotes empty list

    I Example: (cons 1 2 (cons 3 4)) evaluates to (1 2 (3 4))

    I Example:(cons 1 2 (cdr (cons 3 4))) evaluates to (1 2 3)

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 12/30

  • List Operators

    I List operations in LISP are: cons car cdr

    I cons appends two lists or atoms together

    I car,cdr take lists apart: car gives first elem,cdr gives rest

    I nil denotes empty list

    I Example: (cons 1 2 (cons 3 4)) evaluates to (1 2 (3 4))

    I Example:(cons 1 2 (cdr (cons 3 4))) evaluates to (1 2 3)

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 12/30

  • List Operators

    I List operations in LISP are: cons car cdr

    I cons appends two lists or atoms together

    I car,cdr take lists apart: car gives first elem,cdr gives rest

    I nil denotes empty list

    I Example: (cons 1 2 (cons 3 4)) evaluates to (1 2 (3 4))

    I Example:(cons 1 2 (cdr (cons 3 4))) evaluates to (1 2 3)

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 12/30

  • List Operators

    I List operations in LISP are: cons car cdr

    I cons appends two lists or atoms together

    I car,cdr take lists apart: car gives first elem,cdr gives rest

    I nil denotes empty list

    I Example: (cons 1 2 (cons 3 4)) evaluates to (1 2 (3 4))

    I Example:(cons 1 2 (cdr (cons 3 4))) evaluates to (1 2 3)

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 12/30

  • List Operators

    I List operations in LISP are: cons car cdr

    I cons appends two lists or atoms together

    I car,cdr take lists apart: car gives first elem,cdr gives rest

    I nil denotes empty list

    I Example: (cons 1 2 (cons 3 4)) evaluates to (1 2 (3 4))

    I Example:(cons 1 2 (cdr (cons 3 4))) evaluates to (1 2 3)

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 12/30

  • Programs as Data

    I In LISP, programs and data have the same internalrepresentation.

    I Can write LISP programs that build data structuresrepresenting other LISP programs and evaluate them!

    I There is a built-in eval function that calls the Lisp interpreterfrom the Lisp interpreter

    I Example: Suppose we have read function that reads inputfrom user

    I (eval (read)) executes LISP program source code enteredby user!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 13/30

  • Programs as Data

    I In LISP, programs and data have the same internalrepresentation.

    I Can write LISP programs that build data structuresrepresenting other LISP programs and evaluate them!

    I There is a built-in eval function that calls the Lisp interpreterfrom the Lisp interpreter

    I Example: Suppose we have read function that reads inputfrom user

    I (eval (read)) executes LISP program source code enteredby user!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 13/30

  • Programs as Data

    I In LISP, programs and data have the same internalrepresentation.

    I Can write LISP programs that build data structuresrepresenting other LISP programs and evaluate them!

    I There is a built-in eval function that calls the Lisp interpreterfrom the Lisp interpreter

    I Example: Suppose we have read function that reads inputfrom user

    I (eval (read)) executes LISP program source code enteredby user!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 13/30

  • Programs as Data

    I In LISP, programs and data have the same internalrepresentation.

    I Can write LISP programs that build data structuresrepresenting other LISP programs and evaluate them!

    I There is a built-in eval function that calls the Lisp interpreterfrom the Lisp interpreter

    I Example: Suppose we have read function that reads inputfrom user

    I (eval (read)) executes LISP program source code enteredby user!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 13/30

  • Programs as Data

    I In LISP, programs and data have the same internalrepresentation.

    I Can write LISP programs that build data structuresrepresenting other LISP programs and evaluate them!

    I There is a built-in eval function that calls the Lisp interpreterfrom the Lisp interpreter

    I Example: Suppose we have read function that reads inputfrom user

    I (eval (read)) executes LISP program source code enteredby user!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 13/30

  • Dynamic vs. Static Scoping

    I The original version of LISP was dynamically scoped

    I Modern LISP dialects are statically scoped (e.g., Scheme)

    I Statically scoped languages use the textually closest definitionof a variable (source-code based)

    I Dynamically scoped languages use the most recent run-timedefinition of a variable (run-time notion)

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 14/30

  • Dynamic vs. Static Scoping

    I The original version of LISP was dynamically scoped

    I Modern LISP dialects are statically scoped (e.g., Scheme)

    I Statically scoped languages use the textually closest definitionof a variable (source-code based)

    I Dynamically scoped languages use the most recent run-timedefinition of a variable (run-time notion)

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 14/30

  • Dynamic vs. Static Scoping

    I The original version of LISP was dynamically scoped

    I Modern LISP dialects are statically scoped (e.g., Scheme)

    I Statically scoped languages use the textually closest definitionof a variable (source-code based)

    I Dynamically scoped languages use the most recent run-timedefinition of a variable (run-time notion)

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 14/30

  • Dynamic vs. Static Scoping

    I The original version of LISP was dynamically scoped

    I Modern LISP dialects are statically scoped (e.g., Scheme)

    I Statically scoped languages use the textually closest definitionof a variable (source-code based)

    I Dynamically scoped languages use the most recent run-timedefinition of a variable (run-time notion)

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 14/30

  • Dynamic vs. Static Scoping Example

    I Consider the following program:

    (define h (lambda (a) (+ a 1)))

    (define g (lambda (z) ( + (h 0) z))

    (define f (lambda (y) (

    (define h (lambda (a) (+ a 2)))

    (g y))))

    (f 3)

    I In statically-scoped dialect of LISP, what does this programevaluate to?

    4

    I In dynamically-scoped LISP, what does this program evaluateto?

    5

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 15/30

  • Dynamic vs. Static Scoping Example

    I Consider the following program:

    (define h (lambda (a) (+ a 1)))

    (define g (lambda (z) ( + (h 0) z))

    (define f (lambda (y) (

    (define h (lambda (a) (+ a 2)))

    (g y))))

    (f 3)

    I In statically-scoped dialect of LISP, what does this programevaluate to?

    4

    I In dynamically-scoped LISP, what does this program evaluateto?

    5

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 15/30

  • Dynamic vs. Static Scoping Example

    I Consider the following program:

    (define h (lambda (a) (+ a 1)))

    (define g (lambda (z) ( + (h 0) z))

    (define f (lambda (y) (

    (define h (lambda (a) (+ a 2)))

    (g y))))

    (f 3)

    I In statically-scoped dialect of LISP, what does this programevaluate to? 4

    I In dynamically-scoped LISP, what does this program evaluateto?

    5

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 15/30

  • Dynamic vs. Static Scoping Example

    I Consider the following program:

    (define h (lambda (a) (+ a 1)))

    (define g (lambda (z) ( + (h 0) z))

    (define f (lambda (y) (

    (define h (lambda (a) (+ a 2)))

    (g y))))

    (f 3)

    I In statically-scoped dialect of LISP, what does this programevaluate to? 4

    I In dynamically-scoped LISP, what does this program evaluateto?

    5

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 15/30

  • Dynamic vs. Static Scoping Example

    I Consider the following program:

    (define h (lambda (a) (+ a 1)))

    (define g (lambda (z) ( + (h 0) z))

    (define f (lambda (y) (

    (define h (lambda (a) (+ a 2)))

    (g y))))

    (f 3)

    I In statically-scoped dialect of LISP, what does this programevaluate to? 4

    I In dynamically-scoped LISP, what does this program evaluateto? 5

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 15/30

  • Garbage Collection

    I LISP is the first programming language that had the idea ofgarbage collection

    I A memory location is garbage if it can no longer be accessedby the program

    I If a memory location is garbage, changing the value stored inthat location cannot change the result of the program

    I Thus, if we had a way of finding all memory locations that aregarbage, we could reclaim those memory locations and usethem for some other purpose

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 16/30

  • Garbage Collection

    I LISP is the first programming language that had the idea ofgarbage collection

    I A memory location is garbage if it can no longer be accessedby the program

    I If a memory location is garbage, changing the value stored inthat location cannot change the result of the program

    I Thus, if we had a way of finding all memory locations that aregarbage, we could reclaim those memory locations and usethem for some other purpose

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 16/30

  • Garbage Collection

    I LISP is the first programming language that had the idea ofgarbage collection

    I A memory location is garbage if it can no longer be accessedby the program

    I If a memory location is garbage, changing the value stored inthat location cannot change the result of the program

    I Thus, if we had a way of finding all memory locations that aregarbage, we could reclaim those memory locations and usethem for some other purpose

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 16/30

  • Garbage Collection

    I LISP is the first programming language that had the idea ofgarbage collection

    I A memory location is garbage if it can no longer be accessedby the program

    I If a memory location is garbage, changing the value stored inthat location cannot change the result of the program

    I Thus, if we had a way of finding all memory locations that aregarbage, we could reclaim those memory locations and usethem for some other purpose

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 16/30

  • Explicit vs. Automatic Memory Management

    I Garbage collection is the task of detecting garbage during theexecution of a program and reclaiming this unused memory

    I Operating system keeps a list of available memory called thefree list

    I In languages that are not garbage-collected (e.g., C)programmer must explicitly call free to deallocate an unusedmemory location

    I In garbage-collected languages like LISP and Java, theprogrammer does not need to worry about deallocatingmemory – this is done automatically by the run-time system

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 17/30

  • Explicit vs. Automatic Memory Management

    I Garbage collection is the task of detecting garbage during theexecution of a program and reclaiming this unused memory

    I Operating system keeps a list of available memory called thefree list

    I In languages that are not garbage-collected (e.g., C)programmer must explicitly call free to deallocate an unusedmemory location

    I In garbage-collected languages like LISP and Java, theprogrammer does not need to worry about deallocatingmemory – this is done automatically by the run-time system

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 17/30

  • Explicit vs. Automatic Memory Management

    I Garbage collection is the task of detecting garbage during theexecution of a program and reclaiming this unused memory

    I Operating system keeps a list of available memory called thefree list

    I In languages that are not garbage-collected (e.g., C)programmer must explicitly call free to deallocate an unusedmemory location

    I In garbage-collected languages like LISP and Java, theprogrammer does not need to worry about deallocatingmemory – this is done automatically by the run-time system

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 17/30

  • Explicit vs. Automatic Memory Management

    I Garbage collection is the task of detecting garbage during theexecution of a program and reclaiming this unused memory

    I Operating system keeps a list of available memory called thefree list

    I In languages that are not garbage-collected (e.g., C)programmer must explicitly call free to deallocate an unusedmemory location

    I In garbage-collected languages like LISP and Java, theprogrammer does not need to worry about deallocatingmemory – this is done automatically by the run-time system

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 17/30

  • Advantages of Garbage Collection

    I Garbage collection makes programming much easier!

    I In explicitly-memory managed languages, programmer musttrack which memory locations are unused

    I Explicit memory management constant source of headaches:

    I Memory leaks: forgetting to delete unused memory)

    I Dangling pointers: deleting memory that’s still in use

    I In automatically memory-managed languages like LISP andJava, such errors are not possible!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 18/30

  • Advantages of Garbage Collection

    I Garbage collection makes programming much easier!

    I In explicitly-memory managed languages, programmer musttrack which memory locations are unused

    I Explicit memory management constant source of headaches:

    I Memory leaks: forgetting to delete unused memory)

    I Dangling pointers: deleting memory that’s still in use

    I In automatically memory-managed languages like LISP andJava, such errors are not possible!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 18/30

  • Advantages of Garbage Collection

    I Garbage collection makes programming much easier!

    I In explicitly-memory managed languages, programmer musttrack which memory locations are unused

    I Explicit memory management constant source of headaches:

    I Memory leaks: forgetting to delete unused memory)

    I Dangling pointers: deleting memory that’s still in use

    I In automatically memory-managed languages like LISP andJava, such errors are not possible!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 18/30

  • Advantages of Garbage Collection

    I Garbage collection makes programming much easier!

    I In explicitly-memory managed languages, programmer musttrack which memory locations are unused

    I Explicit memory management constant source of headaches:

    I Memory leaks: forgetting to delete unused memory)

    I Dangling pointers: deleting memory that’s still in use

    I In automatically memory-managed languages like LISP andJava, such errors are not possible!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 18/30

  • Advantages of Garbage Collection

    I Garbage collection makes programming much easier!

    I In explicitly-memory managed languages, programmer musttrack which memory locations are unused

    I Explicit memory management constant source of headaches:

    I Memory leaks: forgetting to delete unused memory)

    I Dangling pointers: deleting memory that’s still in use

    I In automatically memory-managed languages like LISP andJava, such errors are not possible!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 18/30

  • Advantages of Garbage Collection

    I Garbage collection makes programming much easier!

    I In explicitly-memory managed languages, programmer musttrack which memory locations are unused

    I Explicit memory management constant source of headaches:

    I Memory leaks: forgetting to delete unused memory)

    I Dangling pointers: deleting memory that’s still in use

    I In automatically memory-managed languages like LISP andJava, such errors are not possible!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 18/30

  • Disadvantages of Garbage Collection

    I Potential disadvantage of garbage collection is overhead

    I In automatically garbage-collected languages, we must dowork at run-time to figure out what is garbage

    I Identifying what is garbage and reclaiming it takes time; thus,there is a performance penalty

    I While the garbage collector is running, program can feelsluggish, unresponsive

    I But for many programmers, this is a small price to pay forinconvenience of manually managing memory

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 19/30

  • Disadvantages of Garbage Collection

    I Potential disadvantage of garbage collection is overhead

    I In automatically garbage-collected languages, we must dowork at run-time to figure out what is garbage

    I Identifying what is garbage and reclaiming it takes time; thus,there is a performance penalty

    I While the garbage collector is running, program can feelsluggish, unresponsive

    I But for many programmers, this is a small price to pay forinconvenience of manually managing memory

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 19/30

  • Disadvantages of Garbage Collection

    I Potential disadvantage of garbage collection is overhead

    I In automatically garbage-collected languages, we must dowork at run-time to figure out what is garbage

    I Identifying what is garbage and reclaiming it takes time; thus,there is a performance penalty

    I While the garbage collector is running, program can feelsluggish, unresponsive

    I But for many programmers, this is a small price to pay forinconvenience of manually managing memory

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 19/30

  • Disadvantages of Garbage Collection

    I Potential disadvantage of garbage collection is overhead

    I In automatically garbage-collected languages, we must dowork at run-time to figure out what is garbage

    I Identifying what is garbage and reclaiming it takes time; thus,there is a performance penalty

    I While the garbage collector is running, program can feelsluggish, unresponsive

    I But for many programmers, this is a small price to pay forinconvenience of manually managing memory

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 19/30

  • Disadvantages of Garbage Collection

    I Potential disadvantage of garbage collection is overhead

    I In automatically garbage-collected languages, we must dowork at run-time to figure out what is garbage

    I Identifying what is garbage and reclaiming it takes time; thus,there is a performance penalty

    I While the garbage collector is running, program can feelsluggish, unresponsive

    I But for many programmers, this is a small price to pay forinconvenience of manually managing memory

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 19/30

  • How Does Garbage Collection Work?

    I In past few decades, lots of research on garbage collection

    I Original LISP garbage collector used simple algorithm calledMark-and-Sweep

    I In Mark-and-Sweep, we reserve a bit for each memory cell,called tag bit, to identify whether it is accessible

    I If tag bit is 1, means cell is in use; if it is 0, it’s unused

    I Mark-and-Sweep has two phases, called mark and sweep

    I In mark phase, garbage collector sets tag bits

    I In sweep phase, garbage collector reclaims cells with tag bit 0

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 20/30

  • How Does Garbage Collection Work?

    I In past few decades, lots of research on garbage collection

    I Original LISP garbage collector used simple algorithm calledMark-and-Sweep

    I In Mark-and-Sweep, we reserve a bit for each memory cell,called tag bit, to identify whether it is accessible

    I If tag bit is 1, means cell is in use; if it is 0, it’s unused

    I Mark-and-Sweep has two phases, called mark and sweep

    I In mark phase, garbage collector sets tag bits

    I In sweep phase, garbage collector reclaims cells with tag bit 0

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 20/30

  • How Does Garbage Collection Work?

    I In past few decades, lots of research on garbage collection

    I Original LISP garbage collector used simple algorithm calledMark-and-Sweep

    I In Mark-and-Sweep, we reserve a bit for each memory cell,called tag bit, to identify whether it is accessible

    I If tag bit is 1, means cell is in use; if it is 0, it’s unused

    I Mark-and-Sweep has two phases, called mark and sweep

    I In mark phase, garbage collector sets tag bits

    I In sweep phase, garbage collector reclaims cells with tag bit 0

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 20/30

  • How Does Garbage Collection Work?

    I In past few decades, lots of research on garbage collection

    I Original LISP garbage collector used simple algorithm calledMark-and-Sweep

    I In Mark-and-Sweep, we reserve a bit for each memory cell,called tag bit, to identify whether it is accessible

    I If tag bit is 1, means cell is in use; if it is 0, it’s unused

    I Mark-and-Sweep has two phases, called mark and sweep

    I In mark phase, garbage collector sets tag bits

    I In sweep phase, garbage collector reclaims cells with tag bit 0

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 20/30

  • How Does Garbage Collection Work?

    I In past few decades, lots of research on garbage collection

    I Original LISP garbage collector used simple algorithm calledMark-and-Sweep

    I In Mark-and-Sweep, we reserve a bit for each memory cell,called tag bit, to identify whether it is accessible

    I If tag bit is 1, means cell is in use; if it is 0, it’s unused

    I Mark-and-Sweep has two phases, called mark and sweep

    I In mark phase, garbage collector sets tag bits

    I In sweep phase, garbage collector reclaims cells with tag bit 0

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 20/30

  • How Does Garbage Collection Work?

    I In past few decades, lots of research on garbage collection

    I Original LISP garbage collector used simple algorithm calledMark-and-Sweep

    I In Mark-and-Sweep, we reserve a bit for each memory cell,called tag bit, to identify whether it is accessible

    I If tag bit is 1, means cell is in use; if it is 0, it’s unused

    I Mark-and-Sweep has two phases, called mark and sweep

    I In mark phase, garbage collector sets tag bits

    I In sweep phase, garbage collector reclaims cells with tag bit 0

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 20/30

  • How Does Garbage Collection Work?

    I In past few decades, lots of research on garbage collection

    I Original LISP garbage collector used simple algorithm calledMark-and-Sweep

    I In Mark-and-Sweep, we reserve a bit for each memory cell,called tag bit, to identify whether it is accessible

    I If tag bit is 1, means cell is in use; if it is 0, it’s unused

    I Mark-and-Sweep has two phases, called mark and sweep

    I In mark phase, garbage collector sets tag bits

    I In sweep phase, garbage collector reclaims cells with tag bit 0

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 20/30

  • Mark-and-Sweep

    I When memory is low, garbage collector initializes tag bits ofall memory cells to 0

    I Then, identifies all memory locations directly used in theprogram, called root nodes

    I Starting from root nodes, we follow all links (i.e., pointers)and change tag bit of each location we accessed to 1

    I This is all there is to the mark phase

    I In the sweep phase, we check tag bit of each memory cell

    I If tag bit of that cell is 0, we add that cell to the free list

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 21/30

  • Mark-and-Sweep

    I When memory is low, garbage collector initializes tag bits ofall memory cells to 0

    I Then, identifies all memory locations directly used in theprogram, called root nodes

    I Starting from root nodes, we follow all links (i.e., pointers)and change tag bit of each location we accessed to 1

    I This is all there is to the mark phase

    I In the sweep phase, we check tag bit of each memory cell

    I If tag bit of that cell is 0, we add that cell to the free list

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 21/30

  • Mark-and-Sweep

    I When memory is low, garbage collector initializes tag bits ofall memory cells to 0

    I Then, identifies all memory locations directly used in theprogram, called root nodes

    I Starting from root nodes, we follow all links (i.e., pointers)and change tag bit of each location we accessed to 1

    I This is all there is to the mark phase

    I In the sweep phase, we check tag bit of each memory cell

    I If tag bit of that cell is 0, we add that cell to the free list

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 21/30

  • Mark-and-Sweep

    I When memory is low, garbage collector initializes tag bits ofall memory cells to 0

    I Then, identifies all memory locations directly used in theprogram, called root nodes

    I Starting from root nodes, we follow all links (i.e., pointers)and change tag bit of each location we accessed to 1

    I This is all there is to the mark phase

    I In the sweep phase, we check tag bit of each memory cell

    I If tag bit of that cell is 0, we add that cell to the free list

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 21/30

  • Mark-and-Sweep

    I When memory is low, garbage collector initializes tag bits ofall memory cells to 0

    I Then, identifies all memory locations directly used in theprogram, called root nodes

    I Starting from root nodes, we follow all links (i.e., pointers)and change tag bit of each location we accessed to 1

    I This is all there is to the mark phase

    I In the sweep phase, we check tag bit of each memory cell

    I If tag bit of that cell is 0, we add that cell to the free list

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 21/30

  • Mark-and-Sweep

    I When memory is low, garbage collector initializes tag bits ofall memory cells to 0

    I Then, identifies all memory locations directly used in theprogram, called root nodes

    I Starting from root nodes, we follow all links (i.e., pointers)and change tag bit of each location we accessed to 1

    I This is all there is to the mark phase

    I In the sweep phase, we check tag bit of each memory cell

    I If tag bit of that cell is 0, we add that cell to the free list

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 21/30

  • Stop the World Collectors

    I Mark-and-sweep is easy to understand, easy to implement

    I But it stops the world: while garbage collector is running,nothing else can happen

    I As a result, program is unresponsive while collector is running

    I People have come up with much more sophisticated garbagecollection algorithms

    I Concurrent garbage collectors run concurrently with theprogram – they don’t “stop the world”

    I But much more difficult to implement and get right – cellscan become reachable while collector is running!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 22/30

  • Stop the World Collectors

    I Mark-and-sweep is easy to understand, easy to implement

    I But it stops the world: while garbage collector is running,nothing else can happen

    I As a result, program is unresponsive while collector is running

    I People have come up with much more sophisticated garbagecollection algorithms

    I Concurrent garbage collectors run concurrently with theprogram – they don’t “stop the world”

    I But much more difficult to implement and get right – cellscan become reachable while collector is running!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 22/30

  • Stop the World Collectors

    I Mark-and-sweep is easy to understand, easy to implement

    I But it stops the world: while garbage collector is running,nothing else can happen

    I As a result, program is unresponsive while collector is running

    I People have come up with much more sophisticated garbagecollection algorithms

    I Concurrent garbage collectors run concurrently with theprogram – they don’t “stop the world”

    I But much more difficult to implement and get right – cellscan become reachable while collector is running!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 22/30

  • Stop the World Collectors

    I Mark-and-sweep is easy to understand, easy to implement

    I But it stops the world: while garbage collector is running,nothing else can happen

    I As a result, program is unresponsive while collector is running

    I People have come up with much more sophisticated garbagecollection algorithms

    I Concurrent garbage collectors run concurrently with theprogram – they don’t “stop the world”

    I But much more difficult to implement and get right – cellscan become reachable while collector is running!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 22/30

  • Stop the World Collectors

    I Mark-and-sweep is easy to understand, easy to implement

    I But it stops the world: while garbage collector is running,nothing else can happen

    I As a result, program is unresponsive while collector is running

    I People have come up with much more sophisticated garbagecollection algorithms

    I Concurrent garbage collectors run concurrently with theprogram – they don’t “stop the world”

    I But much more difficult to implement and get right – cellscan become reachable while collector is running!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 22/30

  • Stop the World Collectors

    I Mark-and-sweep is easy to understand, easy to implement

    I But it stops the world: while garbage collector is running,nothing else can happen

    I As a result, program is unresponsive while collector is running

    I People have come up with much more sophisticated garbagecollection algorithms

    I Concurrent garbage collectors run concurrently with theprogram – they don’t “stop the world”

    I But much more difficult to implement and get right – cellscan become reachable while collector is running!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 22/30

  • LISP and Garbage Collection

    I LISP was first automatically garbage-collected language

    I This idea has been extremely influential

    I Almost any modern language invented today is automaticallymemory managed

    I However, garbage collection does not eliminate all headaches

    I It is still possible to leak memory

    I If you leave references (i.e. pointers) to memory cells thatprogram won’t use, you can still leak memory

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 23/30

  • LISP and Garbage Collection

    I LISP was first automatically garbage-collected language

    I This idea has been extremely influential

    I Almost any modern language invented today is automaticallymemory managed

    I However, garbage collection does not eliminate all headaches

    I It is still possible to leak memory

    I If you leave references (i.e. pointers) to memory cells thatprogram won’t use, you can still leak memory

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 23/30

  • LISP and Garbage Collection

    I LISP was first automatically garbage-collected language

    I This idea has been extremely influential

    I Almost any modern language invented today is automaticallymemory managed

    I However, garbage collection does not eliminate all headaches

    I It is still possible to leak memory

    I If you leave references (i.e. pointers) to memory cells thatprogram won’t use, you can still leak memory

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 23/30

  • LISP and Garbage Collection

    I LISP was first automatically garbage-collected language

    I This idea has been extremely influential

    I Almost any modern language invented today is automaticallymemory managed

    I However, garbage collection does not eliminate all headaches

    I It is still possible to leak memory

    I If you leave references (i.e. pointers) to memory cells thatprogram won’t use, you can still leak memory

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 23/30

  • LISP and Garbage Collection

    I LISP was first automatically garbage-collected language

    I This idea has been extremely influential

    I Almost any modern language invented today is automaticallymemory managed

    I However, garbage collection does not eliminate all headaches

    I It is still possible to leak memory

    I If you leave references (i.e. pointers) to memory cells thatprogram won’t use, you can still leak memory

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 23/30

  • LISP and Garbage Collection

    I LISP was first automatically garbage-collected language

    I This idea has been extremely influential

    I Almost any modern language invented today is automaticallymemory managed

    I However, garbage collection does not eliminate all headaches

    I It is still possible to leak memory

    I If you leave references (i.e. pointers) to memory cells thatprogram won’t use, you can still leak memory

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 23/30

  • Pure vs. Impure LISP

    I The part of LISP we talked about so far is a pure functionallanguage

    I Pure functional language means expressions do not have sideeffects

    I However, for performance/efficiency reasons, it might bedesirable to modify data

    I For example, suppose you need to change value of oneelement in a list

    I In pure LISP, only way to do this is to create a new list that isidentical to old one except for one cell

    I But this can be very inefficient

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 24/30

  • Pure vs. Impure LISP

    I The part of LISP we talked about so far is a pure functionallanguage

    I Pure functional language means expressions do not have sideeffects

    I However, for performance/efficiency reasons, it might bedesirable to modify data

    I For example, suppose you need to change value of oneelement in a list

    I In pure LISP, only way to do this is to create a new list that isidentical to old one except for one cell

    I But this can be very inefficient

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 24/30

  • Pure vs. Impure LISP

    I The part of LISP we talked about so far is a pure functionallanguage

    I Pure functional language means expressions do not have sideeffects

    I However, for performance/efficiency reasons, it might bedesirable to modify data

    I For example, suppose you need to change value of oneelement in a list

    I In pure LISP, only way to do this is to create a new list that isidentical to old one except for one cell

    I But this can be very inefficient

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 24/30

  • Pure vs. Impure LISP

    I The part of LISP we talked about so far is a pure functionallanguage

    I Pure functional language means expressions do not have sideeffects

    I However, for performance/efficiency reasons, it might bedesirable to modify data

    I For example, suppose you need to change value of oneelement in a list

    I In pure LISP, only way to do this is to create a new list that isidentical to old one except for one cell

    I But this can be very inefficient

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 24/30

  • Pure vs. Impure LISP

    I The part of LISP we talked about so far is a pure functionallanguage

    I Pure functional language means expressions do not have sideeffects

    I However, for performance/efficiency reasons, it might bedesirable to modify data

    I For example, suppose you need to change value of oneelement in a list

    I In pure LISP, only way to do this is to create a new list that isidentical to old one except for one cell

    I But this can be very inefficient

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 24/30

  • Pure vs. Impure LISP

    I The part of LISP we talked about so far is a pure functionallanguage

    I Pure functional language means expressions do not have sideeffects

    I However, for performance/efficiency reasons, it might bedesirable to modify data

    I For example, suppose you need to change value of oneelement in a list

    I In pure LISP, only way to do this is to create a new list that isidentical to old one except for one cell

    I But this can be very inefficient

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 24/30

  • Impure Features of LISP

    I Thus, many functional programming languages have anescape hatch to allow modifying data/state

    I Even early versions of LISP had some impure expressionswhich can have side effects

    I Two notable impure expressions in LISP, rplaca and rplacd

    I (rplaca x y) replaces car field of cons cell x with y

    I (rplacd x y) replaces cdr field of cons cell x with y

    I Example: rplaca (cons 1 2) 3) yields the list (1 3)

    I However, we haven’t allocated any new memory – justmodified existing cons cell

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 25/30

  • Impure Features of LISP

    I Thus, many functional programming languages have anescape hatch to allow modifying data/state

    I Even early versions of LISP had some impure expressionswhich can have side effects

    I Two notable impure expressions in LISP, rplaca and rplacd

    I (rplaca x y) replaces car field of cons cell x with y

    I (rplacd x y) replaces cdr field of cons cell x with y

    I Example: rplaca (cons 1 2) 3) yields the list (1 3)

    I However, we haven’t allocated any new memory – justmodified existing cons cell

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 25/30

  • Impure Features of LISP

    I Thus, many functional programming languages have anescape hatch to allow modifying data/state

    I Even early versions of LISP had some impure expressionswhich can have side effects

    I Two notable impure expressions in LISP, rplaca and rplacd

    I (rplaca x y) replaces car field of cons cell x with y

    I (rplacd x y) replaces cdr field of cons cell x with y

    I Example: rplaca (cons 1 2) 3) yields the list (1 3)

    I However, we haven’t allocated any new memory – justmodified existing cons cell

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 25/30

  • Impure Features of LISP

    I Thus, many functional programming languages have anescape hatch to allow modifying data/state

    I Even early versions of LISP had some impure expressionswhich can have side effects

    I Two notable impure expressions in LISP, rplaca and rplacd

    I (rplaca x y) replaces car field of cons cell x with y

    I (rplacd x y) replaces cdr field of cons cell x with y

    I Example: rplaca (cons 1 2) 3) yields the list (1 3)

    I However, we haven’t allocated any new memory – justmodified existing cons cell

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 25/30

  • Impure Features of LISP

    I Thus, many functional programming languages have anescape hatch to allow modifying data/state

    I Even early versions of LISP had some impure expressionswhich can have side effects

    I Two notable impure expressions in LISP, rplaca and rplacd

    I (rplaca x y) replaces car field of cons cell x with y

    I (rplacd x y) replaces cdr field of cons cell x with y

    I Example: rplaca (cons 1 2) 3) yields the list (1 3)

    I However, we haven’t allocated any new memory – justmodified existing cons cell

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 25/30

  • Impure Features of LISP

    I Thus, many functional programming languages have anescape hatch to allow modifying data/state

    I Even early versions of LISP had some impure expressionswhich can have side effects

    I Two notable impure expressions in LISP, rplaca and rplacd

    I (rplaca x y) replaces car field of cons cell x with y

    I (rplacd x y) replaces cdr field of cons cell x with y

    I Example: rplaca (cons 1 2) 3) yields the list (1 3)

    I However, we haven’t allocated any new memory – justmodified existing cons cell

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 25/30

  • Impure Features of LISP

    I Thus, many functional programming languages have anescape hatch to allow modifying data/state

    I Even early versions of LISP had some impure expressionswhich can have side effects

    I Two notable impure expressions in LISP, rplaca and rplacd

    I (rplaca x y) replaces car field of cons cell x with y

    I (rplacd x y) replaces cdr field of cons cell x with y

    I Example: rplaca (cons 1 2) 3) yields the list (1 3)

    I However, we haven’t allocated any new memory – justmodified existing cons cell

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 25/30

  • Quoted Expressions

    I Another interesting feature of LISP is that programmer hasthe freedom to decide whether an expression should beevaluated or not

    I If you quote an expression, written ‘e, this means do notevaluate this expression

    I For example, if you type +1 2, this expression evaluates to 3.

    I But if you write ‘(+ 1 2), it evaluates to + 1 2

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 26/30

  • Quoted Expressions

    I Another interesting feature of LISP is that programmer hasthe freedom to decide whether an expression should beevaluated or not

    I If you quote an expression, written ‘e, this means do notevaluate this expression

    I For example, if you type +1 2, this expression evaluates to 3.

    I But if you write ‘(+ 1 2), it evaluates to + 1 2

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 26/30

  • Quoted Expressions

    I Another interesting feature of LISP is that programmer hasthe freedom to decide whether an expression should beevaluated or not

    I If you quote an expression, written ‘e, this means do notevaluate this expression

    I For example, if you type +1 2, this expression evaluates to 3.

    I But if you write ‘(+ 1 2), it evaluates to + 1 2

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 26/30

  • Quoted Expressions

    I Another interesting feature of LISP is that programmer hasthe freedom to decide whether an expression should beevaluated or not

    I If you quote an expression, written ‘e, this means do notevaluate this expression

    I For example, if you type +1 2, this expression evaluates to 3.

    I But if you write ‘(+ 1 2), it evaluates to + 1 2

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 26/30

  • Summary of LISP’s Notable Features

    I Father of all functional programming languages

    I Supports recursion, higher-order functions, and anonymousfunctions (lambda expressions)

    I Programs and data have same representation: can build datastructures representing LISP programs at run-time andevaluate them

    I Introduced idea of garbage collection

    I All in all, successful and very influential programming language

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 27/30

  • Summary of LISP’s Notable Features

    I Father of all functional programming languages

    I Supports recursion, higher-order functions, and anonymousfunctions (lambda expressions)

    I Programs and data have same representation: can build datastructures representing LISP programs at run-time andevaluate them

    I Introduced idea of garbage collection

    I All in all, successful and very influential programming language

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 27/30

  • Summary of LISP’s Notable Features

    I Father of all functional programming languages

    I Supports recursion, higher-order functions, and anonymousfunctions (lambda expressions)

    I Programs and data have same representation: can build datastructures representing LISP programs at run-time andevaluate them

    I Introduced idea of garbage collection

    I All in all, successful and very influential programming language

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 27/30

  • Summary of LISP’s Notable Features

    I Father of all functional programming languages

    I Supports recursion, higher-order functions, and anonymousfunctions (lambda expressions)

    I Programs and data have same representation: can build datastructures representing LISP programs at run-time andevaluate them

    I Introduced idea of garbage collection

    I All in all, successful and very influential programming language

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 27/30

  • Summary of LISP’s Notable Features

    I Father of all functional programming languages

    I Supports recursion, higher-order functions, and anonymousfunctions (lambda expressions)

    I Programs and data have same representation: can build datastructures representing LISP programs at run-time andevaluate them

    I Introduced idea of garbage collection

    I All in all, successful and very influential programming language

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 27/30

  • Influences of LISP

    I LISP has had a lot of influence on many functionalprogramming languages today

    I Design of ML, Haskell, and many other functional languagesinfluenced by LISP

    I However, arguably LISP does not have the nicest syntax –parentheses and prefix-notation are quite cumbersome

    I Despite this, some wide-used software written in LISP

    I For instance, popular text editor emacs is written in LISP aswell as Linux graphical toolkit gtk

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 28/30

  • Influences of LISP

    I LISP has had a lot of influence on many functionalprogramming languages today

    I Design of ML, Haskell, and many other functional languagesinfluenced by LISP

    I However, arguably LISP does not have the nicest syntax –parentheses and prefix-notation are quite cumbersome

    I Despite this, some wide-used software written in LISP

    I For instance, popular text editor emacs is written in LISP aswell as Linux graphical toolkit gtk

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 28/30

  • Influences of LISP

    I LISP has had a lot of influence on many functionalprogramming languages today

    I Design of ML, Haskell, and many other functional languagesinfluenced by LISP

    I However, arguably LISP does not have the nicest syntax –parentheses and prefix-notation are quite cumbersome

    I Despite this, some wide-used software written in LISP

    I For instance, popular text editor emacs is written in LISP aswell as Linux graphical toolkit gtk

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 28/30

  • Influences of LISP

    I LISP has had a lot of influence on many functionalprogramming languages today

    I Design of ML, Haskell, and many other functional languagesinfluenced by LISP

    I However, arguably LISP does not have the nicest syntax –parentheses and prefix-notation are quite cumbersome

    I Despite this, some wide-used software written in LISP

    I For instance, popular text editor emacs is written in LISP aswell as Linux graphical toolkit gtk

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 28/30

  • Influences of LISP

    I LISP has had a lot of influence on many functionalprogramming languages today

    I Design of ML, Haskell, and many other functional languagesinfluenced by LISP

    I However, arguably LISP does not have the nicest syntax –parentheses and prefix-notation are quite cumbersome

    I Despite this, some wide-used software written in LISP

    I For instance, popular text editor emacs is written in LISP aswell as Linux graphical toolkit gtk

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 28/30

  • L and LISP

    I Our course language L is very similar to LISP, but with nicersyntax

    I In L, parantheses are not as cumbersome and allows infixnotation

    I Also, we also added a static type system and type inference

    I You’ve already implemented an interpreter for a LISP-likefunctional language!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 29/30

  • L and LISP

    I Our course language L is very similar to LISP, but with nicersyntax

    I In L, parantheses are not as cumbersome and allows infixnotation

    I Also, we also added a static type system and type inference

    I You’ve already implemented an interpreter for a LISP-likefunctional language!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 29/30

  • L and LISP

    I Our course language L is very similar to LISP, but with nicersyntax

    I In L, parantheses are not as cumbersome and allows infixnotation

    I Also, we also added a static type system and type inference

    I You’ve already implemented an interpreter for a LISP-likefunctional language!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 29/30

  • L and LISP

    I Our course language L is very similar to LISP, but with nicersyntax

    I In L, parantheses are not as cumbersome and allows infixnotation

    I Also, we also added a static type system and type inference

    I You’ve already implemented an interpreter for a LISP-likefunctional language!

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 29/30

  • A Funny Quote

    ”A LISP programmer knows the value of everything,but the cost of nothing”

    -Alan Perlis

    Thomas Dillig, CS345H: Programming Languages Lecture: LISP 30/30