Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS535 Programming Languages Chapter - 10 Functional Programming With Lists.

Similar presentations


Presentation on theme: "CS535 Programming Languages Chapter - 10 Functional Programming With Lists."— Presentation transcript:

1 CS535 Programming Languages Chapter - 10 Functional Programming With Lists

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

3 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

4 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)

5 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 4 + 5 * 7 is written as ( + 4 (* 5 7) ) w The above uniformity in Lisp syntax makes it easy to manipulate programs as data

6 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

7 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

8 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

9 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 )

10 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

11 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 )

12 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) ) ) )

13 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) ) ) )

14 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 `( 1 2 3 4 5 ) ) Result : (1 4 9 16 25)

15 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

16 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

17 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

18 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

19 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

20 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.

21 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.

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


Download ppt "CS535 Programming Languages Chapter - 10 Functional Programming With Lists."

Similar presentations


Ads by Google