Announcements - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Announcements

Description:

(let ((object (car args)) (message (cdr args))) (let ((try (apply object message))) (if (eq? ... Assume that variable y is free in N and bound in M ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 32
Provided by: sam73
Category:

less

Transcript and Presenter's Notes

Title: Announcements


1
Announcements
  • Test No. 4 Monday (11/13)
  • Functional Programming
  • Closed Book Closed Notes
  • Multiple Choice
  • Challenging

2
Quiz
  • (define deepsum
  • (lambda (list)
  • (cond ((null? list) 0)
  • ((number? (car list))
  • ( (car list) (deepsum (cdr list))))
  • (else ( (deepsum (car list))
  • (deepsum (cdr list)))))))

(deepsum '(((2 (3) (((4)) (4 4) 54 3) 22) ((((4
5)))))))
105
3
Quiz
  • Function bound to variables

(define x (lambda (y) y))
  • Function passed as a parameter

(define f (lambda (x y) (x y))) (f car '(a b c))
  • Function returned as a value

(define f (lambda (a) (lambda (b) ( a
b)))) (f 5) ((f 5) 6)
4
OOP in Scheme
  • Simple example
  • Box object
  • Stores a value
  • State of the box is defined by the value
  • Use a box-maker function to create a box object

5
OOP in Scheme
  • Methods
  • update! Update the value in the box
  • show Show the value stored in the box
  • swap! Swap the value in the box with another
    value
  • reset! Reset the value in the box to the initial
    value
  • type Return the type of the object

6
OOP in Scheme
  • What are boxes?
  • Functions
  • Sending a message to the object would entail
    applying the object to the arguments
  • Simulate message passing by using a send function
  • (send object method-name operands..)

7
Example
  • (define box-a (box-maker ( 3 4)))
  • (define box-b (box-maker 5))
  • (send box-a 'show) 7
  • (send box-b 'show) 5
  • (send box-a 'update! 3)
  • (send box-a 'show) 3
  • (send box-b 'update! (send box-a 'swap! (send
    box-b 'show)))
  • (send box-a 'show) 5
  • (send box-b 'show) 3
  • (send box-a 'reset!)
  • (send box-a 'show) 7
  • (send box-a 'type) box
  • (send box-a 'update 27) Error Bad method name
    update sent
  • to object of box type.

8
OOP in Scheme
  • Methods
  • update! Update the value in the box
  • show Show the value stored in the box
  • swap! Swap the value in the box with another
    value
  • reset! Reset the value in the box to the initial
    value
  • type Return the type of the object

9
Boxmaker
  • (define box-maker
  • (lambda(init-value)
  • (let ((contents init-value))
  • (lambda msg
  • (case (1st msg)
  • ((type) "box")
  • ((show) contents)
  • ((update!) (for-effects-only (set!
    contents (2nd msg))))
  • ((swap!) (let ((ans contents)) (set!
    contents (2nd msg)) ans))
  • ((reset!) (for-effects-only (set! contents
    init-value)))
  • (else (delegate base-object msg)))))))

10
Boxmaker
  • Takes an argument to initialize the value in the
    box
  • Returns a function that takes arbitrary number of
    arguments
  • Parameter list is specified by msg
  • Each invocation of box-maker returns an object
  • E.g. box-a, box-b

11
Auxiliary Functions
  • (define delegate
  • (lambda (obj msg)
  • (apply obj msg)))
  • (define base-object
  • (lambda msg
  • (case (1st msg)
  • ((type) "base-object")
  • (else invalid-method-name-indicator))))

12
Sending Messages
  • (define send
  • (lambda args
  • (let ((object (car args)) (message (cdr
    args)))
  • (let ((try (apply object message)))
  • (if (eq? invalid-method-name-indicator
    try)
  • (error "Bad method name" (car
    message)
  • "sent to object of"
  • (object 'type)
  • "type.")
  • try)))))

13
Error Functions
  • (define error
  • (lambda args
  • (display "Error")
  • (for-each (lambda (value)
  • (display " ")
  • (display value))
  • args)
  • (newline)))

14
Lambda Calculus
  • Square maps real numbers to real numbers

15
Lambda Calculus
  • Foundation of functional programming
  • Developed by Church (1940s)
  • Defines
  • Function parameters
  • Body
  • Does NOT define a name

16
Lambda Calculus
  • Given a lambda expression
  • Application of lambda expression

17
Lambda Calculus
  • Any identifier is a lambda expression
  • If M and N are lambda expressions, then the
    application of M to N, (MN) is a lambda expression

18
Lambda Calculus
Examples
19
Lambda Calculus
Not bound implies free
20
Lambda CalculusSubstitution
  • A substitution of an expression N for a variable
    x in M is written as MN/x
  • If free variables of N have no bound occurrence
    in M,
  • MN/x is formed by replacing all free
    occurrences of x in M by N
  • Else
  • Assume that variable y is free in N and bound in
    M
  • Consistently replace the binding and
    corresponding bound occurrences of y in M by a
    new variable (say u)
  • Repeat this in M until there is no bound
    occurrence
  • Replace all free occurrences in M by N

21
Lambda CalculusSubstitution
22
Lambda CalculusSemantics
  • This is called Beta-Reduction
  • Replace the lambda expression using the
    substitution principle
  • Represents a single function application

23
Lambda CalculusSemantics
  • Evaluation of lambda expression is a sequence of
    beta reductions
  • A lambda expression that does not contain a
    function application is called a normal form

24
Lambda Calculus
  • Functional programming is essentially an applied
    lambda calculus with built in
  • - constant values
  • - functions

25
Lambda Calculus
  • Eager Evaluation
  • Evaluate all expressions ahead of time
  • Irrespective of if it is needed or not
  • May cause some runtime errors
  • Example
  • (if ( x 0) 1 (/ 1 x))

26
Lambda Calculus
  • Lazy Evaluation
  • Evaluate all expressions only if needed
  • Some evaluations may be duplicated
  • Equivalent to call-by-name
  • Allows some types of computations not possible in
    eager evaluation
  • Example
  • Infinite lists
  • List of integers

27
Recursive Functions
  • (define my-reverse
  • (lambda(ls)
  • (cond ((null? ls) '())
  • (else (append (my-reverse (cdr ls))
  • (list (car
    ls)))))))
  • (my-reverse '(a b c d))

28
Recursive Functions
  • (define super-reverse
  • (lambda(ls)
  • (cond ((null? ls) '())
  • ((atom? ls) ls)
  • (else (append (super-reverse (cdr ls))
  • (list (super-reverse (car
    ls))))))))
  • (super-reverse '(((a b) c) ((((d)) e) f g) x))

29
Recursive Functions
  • (define pairup
  • (lambda(x y)
  • (cond ((null? x) '())
  • ((null? y) '())
  • (else (cons (list (car x) (car y))
  • (pairup (cdr x) (cdr
    y)))))))
  • (pairup '(a b c) '(1 2 3))

30
Recursive Functions
  • (define listify
  • (lambda (ls)
  • (cond ((null? ls) '())
  • (else (cons (list (car ls))
  • (listify (cdr ls)))))))
  • (listify '(1 2 2 3))

31
Recursive Functions
  • (define zip
  • (lambda (x y)
  • (cond ((null? x) '())
  • ((null? y) '())
  • (else (cons (car x)
  • (cons (car y)
  • (zip (cdr x) (cdr
    y))))))))
  • (zip '(a b c) '(1 2 3))

32
Recursive Functions
  • (define merge
  • (lambda (x y)
  • (cond ((null? y) x)
  • ((null? x) y)
  • ((lt (car x) (car y))
  • (cons (car x) (merge (cdr x) y)))
  • (else (cons (car y) (merge x (cdr
    y)))))))
  • (merge '(1 4 23 3 7) '(0 9 4 2 4))

33
Recursive Functions
  • (define merge
  • (lambda (x y)
  • (cond ((null? y) x)
  • ((null? x) y)
  • ((lt (car x) (car y))
  • (cons (car x) (merge (cdr x) y)))
  • (else (cons (car y) (merge x (cdr
    y)))))))
  • (merge '(1 4 23 3 7) '(0 9 4 2 4))

34
Recursive Functions
  • (define sort
  • (lambda (x)
  • (cond ((null? x) '())
  • ((null? (cdr x)) x)
  • (else (merge (sort (every-other x))
  • (sort (every-other (cdr
    x))))))))
  • (sort '(0 1 4 9 4 2 4 23 3 7))

35
Summary
  • Pure functional programming
  • No assignments (side effects)
  • Charmingly simple
  • Surprisingly powerful
  • Recursion
  • Functions as first class objects
  • Implicit storage management

36
Summary
  • Managing with functions
  • Lambda function
  • Composition
  • Higher order functions
  • Currying
  • Tail Recursion
  • Dealing with Lists
  • Storage structures
  • Storage management
Write a Comment
User Comments (0)
About PowerShow.com