CS 3015 Midterm Review - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

CS 3015 Midterm Review

Description:

(list (car l) Exercise ... (square (car things) (iter items nil)) Exercise ... (cons ((car fns) item) (apply-all (cdr fns) item)))) Exercise ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 31
Provided by: jeffreyv
Category:
Tags: midterm | review

less

Transcript and Presenter's Notes

Title: CS 3015 Midterm Review


1
CS 3015 Midterm Review
2
Exercise
  • Write a procedure called product that returns the
    product of the values of a function at points
    over a given range.
  • Show how to define factorial in terms of product.

3
Solution
  • (define (product f a next b)
  • (if (gt a b)
  • 1
  • ( (f a)
  • (product f (next a) next b))))
  • (define (fact x)
  • (product (lambda (x) x)
  • 1
  • (lambda (x) ( x 1))
  • x))

4
Problem
  • Write iterative version of product

5
Problem
  • Write iterative version of product
  • (define (product f a next b)
  • (define (prod-iter a result)
  • (if (gt a b) result
  • (prod-iter (next a)
  • ( a result))))
  • (prod-iter a 1))

6
Exercise
  • Define a procedure reverse that takes a list as
    argument and returns a list of the same elements
    in reverse order.

7
Solution
  • Define a procedure reverse that takes a list as
    argument and returns a list of the same elements
    in reverse order.
  • (define (reverse l)
  • (if (null? l) nil
  • (append (reverse (cdr l))
  • (list (car l)))))

8
Exercise
  • Modify your reverse procedure to produce a
    deep-reverse procedure that takes a list as
    argument and returns as its value the list with
    its elements reversed and with all sublists
    deep-reversed as well.

9
Solution
  • (define (deep-reverse l)
  • (cond ((null? l) nil)
  • ((pair? l)
  • (append (deep-reverse (cdr l))
  • (list (deep-reverse
  • (car l)))))
  • (else l)))

10
Exercise
  • Write a function that takes a value and a number
    and returns a list of that value repeated that
    number of times.

11
Solution
  • Write a function that takes an value and a number
    and returns a list of that value repeated that
    number of times.
  • (define (repeat v n)
  • (if ( n 1) (list v)
  • (cons v (repeat n (- n 1))))

12
Exercise
  • Write a function that takes a number and returns
    a function that takes a value and returns a list
    of that value repeated that number of times

13
Solution
  • Write a function that take a number and returns a
    function that takes a value and returns a list of
    that value repeated that number of times
  • (define (make-repeater n)
  • (lambda (v)
  • (define (helper n)
  • (if ( n 1) (list v)
  • (cons v (helper (- n 1)))))
  • (helper n)))

14
Exercise
  • If f is a numerical function and n is a positive
    integer, then we can form the nth repeated
    application of f, which is defined to be the
    function whose value at x is f(f(...(f(x))...)).

15
Solution
  • If f is a numerical function and n is a positive
    integer, then we can form the nth repeated
    application of f, which is defined to be the
    function whose value at x is f(f(...(f(x))...)).
  • (define (repeated f n)
  • (lambda (x)
  • (define (helper n)
  • (if (lt n 2) (f x)
  • (f (helper (- n 1))))
  • (helper n)))

16
Exercise
  • If f is a function and dx is some small number,
    then the smoothed version of f is the function
    whose value at a point x is the average of f(x -
    dx), f(x), and f(x dx). Write a procedure
    smooth that takes as input a procedure that
    computes f and a dx and returns a procedure that
    computes the smoothed f.

17
Solution
  • (define (smooth f dx)
  • (lambda (x)
  • (/ ( (f (- x dx))
  • (f x)
  • (f ( x dx)))
  • 3)))

18
Exercise
  • Show how to generate the n-fold smoothed function
    of any given function using smooth and repeated
    from exercise 1.43.
  • (define (nfold f dx n)
  • (repeated (smooth f dx) n)))

19
Exercise
  • Write square-list iteratively
  • (define (square-list items)(define (iter things a
    nswer)  (if (null? things)      answer      (it
    er (cdr things)             (cons (square(car thi
    ngs))                  answer)))) (iter items ni
    l))

20
Exercise
  • Write square-list iteratively
  • (define (square-list items)(define (iter things a
    nswer)  (if (null? things)      answer      (it
    er (cdr things)             (cons (square(car thi
    ngs))                  answer)))) (iter items ni
    l))
  • Is this right?

21
How about the following?
  • (define (square-list items)(define (iter things a
    nswer)  (if (null? things)      answer      (it
    er (cdr things)            (cons answer         
             (square (car things))))))(iter items nil
    ))

22
Exercise
  • Write a procedure apply-all that takes a list of
    procedures and an item as arguments and returns a
    list of results of applying each procedure to the
    item.

23
Solution
  • Write a procedure apply-all that takes a list of
    procedures and an item as arguments and returns a
    list of results of applying each procedure to the
    item.
  • (define (apply-all fns item)
  • (if (null? fns) nil
  • (cons ((car fns) item)
  • (apply-all (cdr fns) item))))

24
Exercise
  • Write a procedure apply-composition that takes as
    arguments a list of procedures that compute f1fn
    and an argument appropriate for fn, and returns
    the value of the composition of those function at
    that argument.

25
Solution
  • (define (apply-composition fns item)
  • (if (null? fns) item
  • ((car fns)
  • (apply-composition (cdr fns) item))
  • )))

26
Exercise
  • Write make-composition
  • (define (make-composition fns)
  • (lambda (x)
  • (define (helper fns)
  • (if (null? fns) x
  • ((car fns) (helper (cdr fns)))
  • ))
  • (helper fns)))

27
Use substitution model
  • ((make-composition (square cube)) 3)
  • Evaluate the components of the expression
  • (make-composition (square cube))
  • (lambda (x)
  • (define (helper fns)
  • (if (null? fns) x
  • ((car fns) (helper (cdr fns)))
  • ))
  • (helper (square cube))))
  • 3 evaluates to 3

28
Substitution model contd
  • Apply the function to its arguments
  • ((lambda (x)
  • (define (helper fns)
  • (if (null? fns) x
  • ((car fns) (helper (cdr fns)))
  • ))
  • (helper (square cube))))
  • 3)

29
  • (define (helper fns)
  • (if (null? fns) 3
  • ((car fns) (helper (cdr fns)))
  • ))
  • (helper (square cube))
  • (if (null? (square cube)) 3
  • ((car (square cube))
  • (helper (cdr (square cube))))))
  • ((car (square cube))
  • (helper (cdr (square cube))))))
  • (square
  • (helper (cube)))

30
  • (square (if (null? (cube)) 3
  • ((car (cube))
  • (helper (cdr (cube))))))
  • (square ((car (cube))
  • (helper (cdr (cube))))))
  • (square (cube (helper ()))))
  • (square (cube (if (null? ()) 3
  • ((car ()) (helper (cdr ())))
  • ))
  • (square (cube 3))
  • 729
Write a Comment
User Comments (0)
About PowerShow.com