Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

Similar documents
Heap storage. Dynamic allocation and stacks are generally incompatible.

Modern Programming Languages. Lecture LISP Programming Language An Introduction

NOTE: Answer ANY FOUR of the following 6 sections:

SOFTWARE ARCHITECTURE 6. LISP

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Languages

FP Foundations, Scheme

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Functional Programming. Pure Functional Languages

CMSC 331 Final Exam Section 0201 December 18, 2000

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS 314 Principles of Programming Languages

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

A Brief Introduction to Scheme (II)

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Functional Programming

Lisp. Versions of LISP

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

Functional Programming. Big Picture. Design of Programming Languages

15 Unification and Embedded Languages in Lisp

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

Lecture Notes on Lisp A Brief Introduction

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Functional Languages. Hwansoo Han

LECTURE 16. Functional Programming

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Introduction to Functional Programming and basic Lisp

Imperative, OO and Functional Languages A C program is

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Compiler Construction

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

4/19/2018. Chapter 11 :: Functional Languages

Compiler Construction

CMSC 331 Final Exam Section 0201 December 18, 2000

Functional programming in LISP

CS 314 Principles of Programming Languages

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

Functional programming with Common Lisp

CSc 520 Principles of Programming Languages

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

User-defined Functions. Conditional Expressions in Scheme

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

Chapter 15. Functional Programming Languages

CS 314 Principles of Programming Languages

Lisp Basic Example Test Questions

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

CSC 533: Programming Languages. Spring 2015

Control Flow February 9, Lecture 7

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

CS558 Programming Languages

Intro. Scheme Basics. scm> 5 5. scm>

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

LECTURE 17. Expressions and Assignment

Types II. Hwansoo Han

Page # Expression Evaluation: Outline. CSCI: 4500/6500 Programming Languages. Expression Evaluation: Precedence

CS 360 Programming Languages Interpreters

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

G Programming Languages - Fall 2012

Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator.

Expressions and Assignment

A Small Interpreted Language

CS 314 Principles of Programming Languages. Lecture 16

6.001 Notes: Section 8.1

Garbage Collection. Akim D le, Etienne Renault, Roland Levillain. May 15, CCMP2 Garbage Collection May 15, / 35

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

ALISP interpreter in Awk

Functional Programming Languages (FPL)

G Programming Languages - Fall 2012

Introduction to Scheme

CS508-Modern Programming Solved MCQ(S) From Midterm Papers (1 TO 22 Lectures) BY Arslan

Programming Languages

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

Organization of Programming Languages CS3200/5200N. Lecture 11

Scheme: Expressions & Procedures

A LISP Interpreter in ML

Chapter 15 Functional Programming Languages

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

CS 415 Midterm Exam Fall 2003

Documentation for LISP in BASIC

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Concepts Introduced in Chapter 7

CSCI337 Organisation of Programming Languages LISP

MIDTERM EXAMINATION - CS130 - Spring 2003

CS 242. Fundamentals. Reading: See last slide

Common LISP Tutorial 1 (Basic)

CSc 520. Principles of Programming Languages 7: Scheme List Processing

Common LISP-Introduction

COP4020 Programming Assignment 1 - Spring 2011

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Names, Bindings, Scopes

Transcription:

Fifth Generation CS 4100 LISP From Principles of Programming Languages: Design, Evaluation, and Implementation (Third Edition, by Bruce J. MacLennan, Chapters 9, 10, 11, and based on slides by Istvan Jonyer Skip 4th generation: ADA Data abstraction Concurrent programming Paradigms Functional: ML, Lisp Logic: Prolog Object Oriented: C++, Java 1 2 Chapter 9: List Processing: LISP History of LISP McCarthy at MIT was looking to adapt high-level languages (Fortran to AI - 1956 AI needs to represent relationships among data entities Linked lists and other linked structures are common Solution: Develop list processing library for Fortran Other advances were also made IF function: X = IF(N.EQ. 0, ICAR(Y, ICDR(Y List processing and conditional statement combined What do we need? Recursive list processing functions Conditional expression First implementation IBM 704 Demo in 1960 Common Lisp standardized 3 4 Example LISP Program (defun make-table (text table (if (null text table (make-table (cdr text (update-entry table (car text Central Idea: Function Application There are 2 types of languages Imperative Like Fortran, Algol, Pascal, C, etc. Routing execution from one assignment statement to another Applicative LISP Applying a function to arguments (f a 1 a 2 a n No need for control structures Called S-expressions (Symbolic 5 6 1

Prefix Notation Prefix notation is used in LISP Sometimes called Polish notation (Jan Lukasiewicz Operator comes before arguments (plus 1 2 same as 1 + 2 in infix (plus 5 4 7 6 8 9 Functions cannot be mixed because of the list structure (As in Algol: 1 + 2 3 LISP is fully parenthesized No need for precedence rules cond Function (cond ((null x 0 ((eq x y (f x (t (g y Equivalent to if null(x then 0 elsif x = y then f(x else g(y 7 8 Function Definition (defun make-table (text table (if (null text table (make-table (cdr text (update-entry table (car text Function definition is achieved by calling a function( called defun, with arguments Name (make-table Parameters (text table Everything Is a List Why is everything a list in LISP? Simplicity Principle A language should be as simple as possible. There should be a minimum number of concepts, with simple rules for their combination. If there is only one basic mechanism in the language, the language is easier to learn, understand, and implement. Body (if 9 10 The List is the Data Structure Lists contain symbolic data (set text (to be or not to be Lists like (to be or not to be can be manipulated like numbers in other languages (compared, concatenated, split, passed to functions, Atoms The list (to be or not to be has 4 atoms to, be, or, not Functions are provided for manipulation of atoms Lists of lists ((to be or not to be (that is the question 11 Programs Are Lists Programs are also represented as lists (make-table text nil Can be a list with atoms make-table, text, and nil Can be a function make-table with 2 arguments How do we tell apart the program from a data list? Quoted lists are not interpreted: (set text (to be or not to be Unquoted ones are interpreted (set text (to be or not to be (function: to 12 2

Implications? If programs are lists and data is also list then we can generate a list that can be interpreted as a program In other words We can write a program to write and execute another program Useful in artificial intelligence Reductive aspects? LISP Is Interpreted Most LISP systems provide interactive interpreters One can enter commands into the interpreter, and the system will respond > (plus 2 3 5 > (eq (plus 2 3 (difference 9 4 t (means true 13 14 Pure vs Pseudo-Functions Pure functions plus, eq, Only effect is the computation of a value Pseudo-functions Has side-effect; more like a procedure set (set text (to be or not to be Side effect: Sets the value of text to (to be or not to be Return value: (to be or not to be Data Structures Primitives Numbers Operations: plus, minus, times, eq, etc. Non-numeric atoms Strings of characters used as symbols Much like enumerated types in Pascal Not used as strings Operations: eq Special atoms t: true nil: false; non-existent atom; empty list 15 16 Data Constructor The data constructor is the list Lists can have 0, 1 or more elements Observes the Zero-One-Infinity principle Empty list: ( or nil All lists are non-atomic (except empty list > (atom ( or (atom nil or (atom 5 t > (atom (to be or (atom (( nil 17 Car and Cdr Accessing parts of a list Car Accesses first element of the list >(car (to be or not to be to >(car ((to be or (not to be (to be Returns an element cdr Accesses rest of the list (list without first element >(cdr (to be or not to be (be or not to be Returns a list 18 3

Combining car and cdr Defining Functions How do we select the second element? >(car (cdr (to be or not to be be Third? >(car (cdr (cdr (to be or not to be or How about this? (set DS ( (Don Smith 45 30000 (Aug 4 80 Select day of hire >(car (cdr (car (cdr (cdr (cdr DS 4 This can be simplified: >(cadadddr DS 4 19 (set DS ( (Don Smith 45 30000 (Aug 4 80 Define functions to replace cadadddr (defun hire-date (r (cadddr r (defun day (d (cadr d Now we can select the day of the hire date as (day (hire-date DS This is more readable and more maintainable 20 Property Lists List like this are hard to maintain and read: ((Don Smith 45 30000 (Aug 4 80 We don t know what elements mean Hard to change the structure of the list A better way is to use property lists: (name (Don Smith age 45 salary 30000 hire-date (Aug 4 80 This way we can search for property name we want (age and return value (45 Order of properties becomes immaterial General form (p 1 v 1 p 2 v 2 p n v n 21 Accessing Property Lists (name (Don Smith age 45 salary 30000 hire-date (Aug 4 80 How do we find the property? If property we want is the first one, return second element of list else skip first 2 elements, and start over In LISP (get property p of list l (defun getprop (p l (if (eq (car l p (cadr l (getprop p (cddr l 22 Association Lists What if the property does not have a value? (e.g. retired What is the property has more than a single value? Of course, these can be solved using the property list, if we understand the properties of each property A better, more foolproof way is to use association-lists: ( (name (Don Smith (age 45 (salary 30000 (hire-date (Aug 4 80 23 Constructing Lists Need inverse of car and cdr car: get first of list cdr: get rest of list Inverse: cons: append first of list to rest of list >(cons to (be or not to be (to be or not to be >(cons (to be (or not to be ((to be or not to be Returns a list 24 4

Appending Lists >(cons (to be (or not to be ((to be or not to be But we d like (to be or not to be >(append (to be (or not to be (to be or not to be How would we implement append? We need to extract and cons the last element of the first list successively (defun append (L M (if (null L M (cons (car L (append (cdr L M 25 [3]> (defun mappend (L M (if (null L M (cons (car L (mappend (cdr L M MAPPEND [4]> (trace mappend ;; Tracing function MAPPEND. (MAPPEND [5]> (mappend '(to be '(or not to be 1. Trace: (MAPPEND '(TO BE '(OR NOT TO BE 2. Trace: (MAPPEND '(BE '(OR NOT TO BE 3. Trace: (MAPPEND 'NIL '(OR NOT TO BE 3. Trace: MAPPEND ==> (OR NOT TO BE 2. Trace: MAPPEND ==> (BE OR NOT TO BE 1. Trace: MAPPEND ==> (TO BE OR NOT TO BE (TO BE OR NOT TO BE 26 Atoms Adding Properties to Atoms LISP was written for AI to represent complex relationships among objects Objects can have many properties in real life; Atoms allow for modeling this Each atom comes with its own property list, and some built-in properties pname (print name; mandatory apval (applied value; to store data If atom is bound to a value expr (expression; to store program If atom is bound to a program 27 Other, arbitrary properties may also be added to an atom using putprop (not in our clisp: setf (putprop atom propvalue propname (putprop France Paris capital Paris, in this case, is also an atom Find out the value of a property using get >(get France capital Paris >(get France pname France 28 Special Property: apval Assigning a value to an atom (set Europe (England France is the same as (putprop Europe (England France apval Applied value points to the list the atom is bound to List Representation Lists are represented as linked lists (to be or not to be to be or not to be nil ((to 2 (be 2 / / / to 2 be 2 29 30 5

Origins of car and cdr First LISP was designed for the IBM 704 1 word had 2 fields Address field Decrement field car: Content of Address part of Register cdr: Content of Decrement part of Register car cdr Implementation of cons car and cdr simply return the respective parts of the register cons has the job of constructing a new register using two pointers Allocate new memory location Fill in left and right parts of new location (cons to (be or not to be to be or 31 to be or not to be nil 32 Sublists Can Be Shared (set L (or not to be (set M (to be (set N (cons (cadr M L (set O (cons (car M N M L O to / be or not to N be / [10]> (set 'L '(or not to be (OR NOT TO BE [11]> (set 'M '(to be (TO BE [12]> (set 'N (cons (cadr M L (BE OR NOT TO BE [13]> (set 'O (cons (car M N (TO BE OR NOT TO BE 33 34 List Structures Can Be Modified Functions discussed so far do not modify lists Modifying lists is possible via replaca (replace address part replacd (replace decrement part It is possible that more than one symbol points to a list which can be modified using replaca and replacd This can cause unexpected problems (like Iteration by Recursion Iteration is done by recursion Iteration is mostly needed to perform an operation on every element of a list This can be done using combination of testing for end of list, operating on first element, and recursing on rest of the list (defun plus-red (a (if (null a nil (plus (car a (plus-red (cdr a Notice: No array bounds are needed Function is very general 35 36 equivalence in Fortran 6

Iteration = Recursion Theoretically, recursion and iteration have the same power, and are equivalent One can be translated to the other (although may not be practical Recursion à iteration Use iteration and keep track of auxiliary information in an explicit stack Iteration à recursion Need to pass control information (variables 37 Storage Reclamation What happens to cons d pointers that are no longer in use? Explicit reclamation is the obvious / traditional way C: malloc, calloc, realloc, free C++: new, delete Pascal: new, dispose Issues Complicates programming Requires the programmer to keep track of pointers Violates security of the environment Memory freed, but still referenced (dangling pointers 38 Automatic Storage Reclamation It would be nice for the system to automatically reclaim storage no longer used System can keep track of number of references to storage When references decrease to 0, storage is returned to free-list Advantage: Storage reclaimed immediately as last reference is destroyed Disadvantage: Cyclic structures (points to itself cannot be 39 reclaimed Garbage Collection A different approach is garbage collection Do not keep track of references to location When last reference is destroyed, we still do not do anything, and leave the memory as garbage (unused, non-reusable storage, littering the memory Collect garbage if system runs out of storage Mark all areas unused Then examine all visible pointers and mark storage they point to as used Leftover is garbage, and can be put on free-list This is called the mark-and-sweep method 40 Garbage Collection Advantages Fast until runs out of memory No additional memory is needed for tracking references Disadvantages Garbage collection itself can be slow If memory is large, and have many references Must halt entire system, since all dynamic memory must be marked as unused first Java uses this approach 41 7