Lisp Internals. 2 A problem with lists In Lisp, a list may “contain” another list –For example, (A (B C)) contains (B C) So, how much storage do we need.

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Compiling Web Scripts for Apache Jacob Matthews Luke Hoban Robby Findler Rice University.
Identity and Equality Based on material by Michael Ernst, University of Washington.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
23-Apr-15 Abstract Data Types. 2 Data types I We type data--classify it into various categories-- such as int, boolean, String, Applet A data type represents.
PZ10B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10B - Garbage collection Programming Language Design.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
LISP Programming. LISP – simple and powerful mid-1950’s by John McCarthy at M.I.T. “ LIS t P rocessing language” Artificial Intelligence programs LISP.
CS 1114: Data Structures – Implementation: part 1 Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Access to Names Namespaces, Scopes, Access privileges.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester, 2010
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
PRACTICAL COMMON LISP Peter Seibel 1.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Mitthögskolan 10/8/ Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it.
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Advanced topic - variable.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Halting Problem Introduction to Computing Science and Programming I.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
1 Scheme (Section 11.2) CSCI 431 Programming Languages Fall 2003.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
CS314 – Section 5 Recitation 9
Functional Programming
Computer Organization and Design Pointers, Arrays and Strings in C
Introduction to Computing Science and Programming I
ML: a quasi-functional language with strong typing
University of Central Florida COP 3330 Object Oriented Programming
Lists in Lisp and Scheme
Namespaces, Scopes, Access privileges
Stacks, Queues, and Deques
Stacks, Queues, and Deques
J.E. Spragg Mitthögskolan 1997
<INSERT_WITTY_QUOTE_HERE>
Linked Lists.
Java Programming Language
Data Structures & Algorithms
Stacks, Queues, and Deques
slides created by Ethan Apter and Marty Stepp
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Lisp.
Lists in Lisp and Scheme
Week 9 - Monday CS222.
Presentation transcript:

Lisp Internals

2 A problem with lists In Lisp, a list may “contain” another list –For example, (A (B C)) contains (B C) So, how much storage do we need to allocate for a list? –If any list can contain any other list, there is no limit to the size of storage block we may need –This is impractical; we need another solution

3 Pointers Instead of actually putting one list inside another, we put a pointer to one list inside another –A pointer is a fixed, known size This partially solves the problem, but... –A list can contain any number of elements –For example, the list ((A)(B)(A)(C)) contains four lists –This still leaves us needing arbitrarily large blocks of storage

4 CAR and CDR We can describe any list as the sum of two parts: its “head” (CAR) and its “tail” (CDR) –The head is the first thing in the list –The head could itself be an arbitrary list –The tail of a list is another (but shorter) list Thus, any list can be described with just two pointers This provides a complete solution to our problem of arbitrarily large storage blocks

5 S-expressions In Lisp, everything is an S-expression An S-expression is an atom or a list You can think of these as using two different kinds of storage locations--one kind for atoms, another kind for the parts of a list –This is an oversimplification, but it will do for now

6 Atoms An atom is a simple thing, and we draw it in a simple way: Sometimes we don’t bother with the boxes: HELLO ABCNIL HELLO ABCNIL

7 Lists A list has two parts: a CAR and a CDR We draw this as a box, called a cons cell, with two compartments, called the car field and the cdr field In each of these compartments we put an arrow pointing to its respective value: car fieldcdr fieldcar field value of carvalue of cdr

8 Example I ( A ) A NIL A

9 Example II ( A B C ) A(B C)B(C)CNIL ABC

10 Example III BA NIL ((A) B)

11 Example IV B A NIL C D ((A B) (C D))

12 Dotted pairs In a simple list, every right-pointing arrow points to a cons cell or to NIL If a right-pointing arrow points to an atom, we have a dotted pair (A. B) A B

13 Lisp lists are implemented with dotted pairs Therefore, (A) = (A. NIL) All structures in Lisp can be created from atoms and dotted pairs ( A ) = A NIL = (A. NIL)

14 Example V A B C D ((A. B). (C. D))

15 Writing dotted pairs A dotted pair is written (and printed) using parentheses and a dot: (A. B) If the CDR of a dotted pair is NIL, the dot and the NIL are omitted: (A. NIL) = (A) If the CDR is another cons cell, Lisp doesn’t print the dot or the parentheses –(A. (B. (C. NIL))) = (A B C) –(A. (B. (C. D))) = (A B C. D)

16 Efficiency of CDR Suppose L is the list (A B C D E) Then (CDR L) is the list (B C D E) Isn’t it expensive to create this new list? Answer: NO! It’s incredibly cheap! Lisp just copies a pointer: A(B C D E) L (CDR L)

17 Efficiency of CAR and CONS CAR is just like CDR ; you just copy a pointer CONS takes more work; you have to allocate and fill one cons cell A Here’s the atom A B NIL Here’s the list (B) Here’s the cons cell we add to create the list (A B)

18 Sharing structure List L and list (CDR L) are said to share structure But if L = (A B C D E) and M = (CDR L), then when you change L, won’t M be changed? Yes, but... –this is where the real genius of Lisp comes in... You never change L ! None of the basic functions ever change anything that’s already there Only CONS adds anything The result is an extraordinarily efficient language!

19 Memory If you only add structure, and never change or delete anything, won’t you run out of memory? Lisp uses garbage collection to recover structures that you are no longer using –More convenient for the programmer –Safer (less subject to human error) –Extremely effective in general

20 Java isn’t Lisp Although Lisp’s way of handling lists is elegant and efficient, it’s not the only way –Modifying the middle or end of a list is expensive There are many ways we might implement lists in Java Lisp’s implementation of lists is a great example, but not necessarily the final word

21 A possible Java implementation class Cell { } class Atom extends Cell { String value; Atom(String value) { // constructor this.value = value; } } class ConsCell extends Cell { Cell car; Cell cdr; ConsCell(Cell car, Cell cdr) { // constructor this.car = car; this.cdr = cdr; } }

22 Another possible implementation class Cell { boolean isAtom; String value; Cell car, cdr; Cell(String value) { // constructor isAtom = true; this.value = value; } Cell(Cell car, Cell cdr) { // constructor isAtom = false; this.car = car; this.cdr = cdr; } }

23 Implementing the functions I class Lisp { static Cell car(Cell c) { return c.car; } static Cell cdr(Cell c) { return c.cdr; } static Cell cons(Cell c1, Cell c2) { return new Cell(c1, c2); }

24 Implementing the functions II static boolean atom(Cell c) { return c.isAtom; } static boolean eq(Cell c1, Cell c2) { return c1.value.equals(c2.value); } }

25 The End