Title: Functional%20Programming%20with%20Lists
1 CS535 Programming
Languages Chapter -
10 Functional Programming
With Lists
2Outline
- Functional Language LISP
- Scheme, a Dialect of LISP
- The structure of lists
- List manipulation
-
- Storage allocation for lists
3Functional 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
4Scheme, A Dialect Of LISP
- A scheme is a small language that provides
- constructs at the core of Lisp
- Constructs include
- 1. Conditionals
- 2. The let construct
- 3. Quoting (data in the form of expressions)
5Expression
- Scheme uses a form of prefix notation for
- expressions
- The general form of an expression in Scheme is
- ( E1 E2 E3 E?)
- The expression 4 5 7 is written as
- ( 4 ( 5 7) )
- The above uniformity in Lisp syntax makes
- it easy to manipulate programs as data
-
6Function Definition
- The recursive and non recursive function
definition associates a function value - with a name
- The general syntax of function definition
- ( define (ltfunction namegt ltargumentsgt)
ltexpressiongt) - An example
- ( define ( Square X ) ( X X ) )
- (Square 5) 25
7 Conditionals
- Conditional expressions come in two forms
-
- 1. (if P E1 E2)
- 2. ( cond ( P1 E1) .
- ( Pn E n )
- ( else E n 1 ) )
- Conditionals are generally required for
- recursive functions
8 let Construct
- The general syntax of the let construct is
- ( let ( (X1 E1 ) (X 2 E 2) (X k E k)
) F ) - For instance (33) (44) is written as
- ( let ( (three-sq(square 3))
- (four-sq(square 4) )
- ( three -sq four-sq) )
- 25
9Quoting
- Used to choose a spelling as a symbol or a
variable name - Two methods of quoting are
- 1. ( quote ltitemgt )
- 2. ltitemgt
- ( define f ) // Unquoted
- ( f 2 3 ) 6
- ( define f ) //Quoted represents symbol which
is a bad procedure - ( f 2 3 )
10The Structure of Lists
- List 1 (this is (a list) of elements)
this
is
Fig1 Tree representation of the
structure of List 1 - () are important
a
( ) nil
list
of
elements
( )
11Operations 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 )
12List 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) ) ) - )
- )
-
132. 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) ) ) ) )
143. 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)
15Storage 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
-
16Consider the list formed from the
expressions 1. ( cons it (cons seems (cons
that () ) ) )
List formed (it seems that)
X
( )
seems
that
it
Fig2 List X created by executing the above
expression
172. ( cons (car X) (cdr X) )
List Y formed (it seems that)
Y
X
( )
seems
it
that
Fig 3 List Y created by executing the above
expression
18Allocation and Deallocation
- Cells no longer in use have to be deallocated
- to avoid the problems of memory shortage
- A standard technique is to link the cells on a
list called a free list - A free list acts as a stack of cells on which the
- standard PUSH and POP operations are
performed - A language implementation performs the garbage
collection when it returns cells to the - free list
19Approaches 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.
21Implementation of garbage collection
- 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.
22Equal? And Eq?
- Equal? - value equality
- eq? - pointer equality