CS535 Programming Languages Chapter - 10 Functional Programming With Lists.

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.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages It’s elegant, minimal, can be.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
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.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
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.
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
Functional Programming Chapter 14. History of Functional Languages Lisp is the second oldest language Motivated by a need to do symbolic, rather than.
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 15 Functional Programming Languages.
Λ => Scheme for Rubyists. Scheme History Authors: Guy Steele and Gerald Sussman Structure and Interpretation of Computer Programs (SICP) by Abelson &
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Comparative Programming Languages Functional programming with Lisp/Scheme.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
1 Programming Languages (CS 550) List Processing, Dynamic Memory Allocation and Garbage Collection Jeremy R. Johnson.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
CS314 – Section 5 Recitation 9
Functional Programming
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming
Tail Recursion.
CS 550 Programming Languages Jeremy Johnson
CS 326 Programming Languages, Concepts and Implementation
Racket CSC270 Pepper major portions credited to
CS 326 Programming Languages, Concepts and Implementation
Chapter 15 – Functional Programming Languages
Concepts of programming languages
Lists in Lisp and Scheme
Closures and Streams cs784(Prasad) L11Clos
CS 270 Math Foundations of CS Jeremy Johnson
PZ10CX - LISP Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section Appendix A.6.
Mini Language Interpreter Programming Languages (CS 550)
FP Foundations, Scheme In Text: Chapter 14.
CS 36 – Chapter 11 Functional programming Features Practice
Modern Programming Languages Lecture 20 Fakhar Lodhi
6.001 SICP Variations on a Scheme
Announcements Quiz 5 HW6 due October 23
List Allocation and Garbage Collection
List Allocation and Garbage Collection
Modern Programming Languages Lecture 18 Fakhar Lodhi
Lisp.
More Scheme CS 331.
Presentation transcript:

CS535 Programming Languages Chapter - 10 Functional Programming With Lists

Outline Functional Language: LISP Scheme, a Dialect of LISP The structure of lists List manipulation Storage allocation for lists

Functional Language: LISP Includes all the basic concepts of functional programming First designed in 1958 by John McCarthy Functions include recursion, first-class functions and garbage collection Lisp implementation led the way in integrated programming environments

Scheme, A Dialect Of LISP w A scheme is a small language that provides constructs at the core of Lisp w Constructs include: 1. Conditionals 2. The “ let ” construct 3. Quoting (data in the form of expressions)

Expression w Scheme uses a form of prefix notation for expressions w The general form of an expression in Scheme is ( E1 E2 E3 …… Eη) w The expression * 7 is written as ( + 4 (* 5 7) ) w The above uniformity in Lisp syntax makes it easy to manipulate programs as data

Function Definition w The recursive and non recursive function definition associates a function value with a name w The general syntax of function definition: ( define ( ) ) w An example: ( define ( Square X ) ( * X X ) ) ; (Square 5) = 25

Conditionals w Conditional expressions come in two forms 1. (if P E1 E2) 2. ( cond ( P1 E1) …. ( P n E n ) ( else E n + 1 ) ) w Conditionals are generally required for recursive functions

let Construct w The general syntax of the let construct is ( let ( (X 1 E 1 ) (X 2 E 2 ) … (X k E k ) ) F ) w For instance (3*3) + (4*4) is written as ( let ( (three-sq(square 3)) (four-sq(square 4) ) (+ three -sq four-sq) ) 25

Quoting w Used to choose a spelling as a symbol or a variable name w Two methods of quoting are: 1. ( quote ) 2. ` w ( define f * ) // Unquoted ( f 2 3 ) 6 w ( define f `* ) // Quoted represents symbol which is a bad procedure ( f 2 3 )

The Structure of Lists List 1 : (this is (a list) of elements) this is a list ( ) nil of elements ( ) Fig1: Tree representation of the structure of List 1 - () are important

Operations on Lists List X: () List Y: ( this is (a list) of elements) Basic Operation Result 1. (null? X) # t (rue) 2. ( car Y ) this (first element) 3. ( cdr X ) (is (a list) of elements) (rest of the list after the first is removed) 4. (car (cdr X) ) is 5. (cdr (cdr X) ) ( (a list) of elements ) 6. (cons a X ) ( a )

List Manipulation 1. Length function: The equation for non empty list X : (length x) = (+ 1 (length (cdr x) ) ) Corresponding length function definition: ( define (length X) ( cond ( (null? X) 0) (else (+ 1 ( length (cdr x) ) ) )

2. Append function: The equation for appending two lists is: (append X Z) = (cons (car X) (append (cdr X) Z)) Corresponding append function definition: (define (append X Z) ( cond ( ( null? X) Z) (else (cons (car X) (append (cdr X) Z) ) ) )

3. Mapping a function A function f applied to a single list element can be extended using map and applied to all elements of the same list Eg: Considering the square function, ( define (square n) (* n n ) ) ( map square `( ) ) Result : ( )

Storage allocation for lists Implementation of lists in Scheme and ML is usually done by using cells. Each cell holds pointers to the head and tail or car and cdr, respectively The cons operation allocates a single cell Each execution of cons returns a pointer to a newly allocated cell

Consider the list formed from the expressions: 1. ( cons `it (cons `seems (cons `that `() ) ) ) it seemsthat ( ) Fig2: List X created by executing the above expression List formed : (it seems that) X

2. ( cons (car X) (cdr X) ) List Y formed: (it seems that) it seems that ( ) Fig 3: List Y created by executing the above expression Y X

Allocation and Deallocation w Cells no longer in use have to be deallocated to avoid the problems of memory shortage w A standard technique is to link the cells on a list called a free list w A free list acts as a stack of cells on which the standard PUSH and POP operations are performed w A language implementation performs the garbage collection when it returns cells to the free list

Approaches to Deallocation of cells 1. Lazy approach: Deallocation starts only after all the memory is exhausted, after which all the dead cells are collected. This method is time consuming Possibility of interrupting the ongoing processing

2. Eager approach: Deallocating is done by checking a cell for any future requirement in an operation The cell is then placed on the free list to be freshly allocated on a POP operation Standard technique is to reserve some space in each cell for a reference count of the number of pointers to the cell.

Implementation of garbage collection w The Mark Sweep Approach: 1. Mark phase involves marking all the cells which can be reached by following pointers. 2. Sweep phase involves sweeping through the memory, looking for any unmarked cells. 3. The sweeping phase starts at one end of the memory and looks at every cell 4. Once identified, all the unmarked cells are sent to the free list for fresh allocation.

Equal? And Eq? w Equal? - value equality w eq? - pointer equality