Functional Programming: Lisp - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Functional Programming: Lisp

Description:

(plus (car a) (plus-red (cdr a)) )) ) (plus-red (1 2 3 4 5)) 15. 7. Mapping: mapping a list into another list of the same size (defun add1-map (a) (if (null a) ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 20
Provided by: eceU3
Category:

less

Transcript and Presenter's Notes

Title: Functional Programming: Lisp


1
Functional Programming Lisp
  • MacLennan Chapter 10

2
Control Structures
  • Atoms are the only primitives since they are the
    only constructs that do not alter the control
    flow.
  • Literals (represent themselves)
  • Numbers
  • Quoted atoms
  • lists
  • Unquoted atoms are bound to
  • Functions (if they have expr property)
  • Data values (if they have apval property)
  • Control-structure constructors
  • Conditional expression
  • Recursive application of a function to its
    arguments

3
  • In LISP we have conditional expression.
  • While in Fortran and Pascal we have to drop from
    expression level to instruction level to make a
    choice.

4
The logical connectives are evaluated
conditionally
  • (or x y) (if x t y)
  • (or (eq (car L) key) (null L) )
  • What happen if L is null?
  • (or (null L) (eq(car L) key) )
  • Does this work?
  • (if (null L) t (eq (car L) key))

5
Iteration is done by recursive
  • (defun getprop (p x)
  • (if (eq (car x) p)
  • ( cadr x)
  • (getprop p (cddr x)) ))

6
  • Reduction reducing a list to one value
  • (defun plus-red (a)
  • (if (null a)
  • 0
  • (plus (car a) (plus-red (cdr a)) )) )
  • (plus-red (1 2 3 4 5))
  • gtgt 15

7
  • Mapping mapping a list into another list of the
    same size
  • (defun add1-map (a)
  • (if (null a)
  • nil
  • (cons (add1 (car a)) (add1-map (cdr a))
    )) )
  • (add1-map (1 9 8 4))
  • gtgt(2 10 9 5)

8
  • Filtering forming a sublist containing all the
    elements that satisfy some property
  • (defun minus-fil (a)
  • (cond ((null a) nil)
  • (( minusp (car a))
  • (cons (car a) minusp-fil (cdr
    a)) ))
  • (t (minusp-fil (cdr a)) )) )

9
  • Some thing like Cartesian product , which needs
    two nested loop
  • For example
  • (All-pairs (a b c) (x y z))
  • gtgt((a x) (a y) (a z) (b x) (b y) (b z) (c x) (c
    y) (c z))
  • We use distl (distribute from left)
  • (distl b (x y z))
  • gtgt((b x) (b y) (b z))

10
  • (defun all-pairs (M N)
  • (if (null M)
  • nil
  • (append (distl (car M) N)
  • (all-pairs (cdr M) N)) ))
  • (defun distl (x N)
  • (if (null N)
  • nil
  • (cons (list x) (car N))
  • (distl x (cdr N)) )) )
  • (the list function makes a list out of its
    argument)

11
  • Recursion for Hierarchical structures
  • (defun equal (x y)
  • ( or (and (atom x) (atom y) (eq x y))
  • (and (not (atom x)) (not (atom y))
  • (equal (car x) (car y))
  • (equal (cdr x) (cdr y)) )) )

12
  • Recursion and Iteration are theoretically
    equivalent

13
Functional arguments allow abstraction
  • mapcar a function which applies a given function
    to each element of a list and returns a list of
    the results.
  • (defun mapcar (f x)
  • (if (null x)
  • nil
  • (cons (f (car x)) (mapcar f (cdr x)) ))
    )
  • (mapcar add1 (1 9 8 4))
  • gtgt (2 10 9 5)
  • (mapcar zerop (4 7 0 3 -2 0 1))
  • gtgt (nil nil t nil nil t nil)
  • (mapcar not (mapcar zerop (4 7 0 3 -2 0 1)))
  • gtgt (t t nil t t nil t)

14
Functional arguments allow programs to be combined
  • They simplify the combination of already
    implemented programs.

15
Lambda expressions are anonymous functions
  • Instead of defining a name for a function
  • (defun twotimes (x) (times x 2)
  • (mapcar twotimes L)
  • We may write
  • (mapcar (lambda (x) (times x 2)) L)

16
Name Structures
  • The primitive name structures are the individual
    bindings of names (atoms) to their values.
  • Bindings are established through
  • Property lists
  • By pseudo-functions
  • set (like declaring a variable)
  • Defun (like declaring a procedure)
  • Actual-formal correspondence

17
  • Application binds Formals to Actuals

18
  • Temporary bindings are a simple, syntactic
    extension (with let )

19
  • Dynamics scoping is the constructor
  • Dynamic scoping complicates functional arguments
Write a Comment
User Comments (0)
About PowerShow.com