Abstract List Functions - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Abstract List Functions

Description:

Creating anonymous functions. Scheme's built-in ... Can I create temporary, anonymous functions to use and discard? ... names this anonymous function 'add-3-to. ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 15
Provided by: BillL161
Category:

less

Transcript and Presenter's Notes

Title: Abstract List Functions


1
Abstract List Functions
2
Outline
  • Prerequisites
  • Abstraction
  • Functions as parameters
  • Objectives
  • Creating anonymous functions
  • Scheme's built-in list processing
  • Reference
  • HTDP Section 21.2, Intermezzo 4

3
Background
  • We understand that function names can be passed
    as parameters and used to process data
  • We now attack two questions
  • Can I create temporary, anonymous functions to
    use and discard?
  • What standard things might I want to do to create
    and analyze lists?

4
Creating Temporary Functions
  • We normally define functions in the following
    style
  • (define (add-3-to n)
  • ( 3 n))
  • However, for test purposes, we have also used
  • (define test-data (list 'a 'b 'c 'd))
  • Scheme permits the definition of a function in
    manner similar to the second definition
  • (define add-3-to (lambda (n)
  • ( 3 n)) )

5
Creating Temporary Functions
  • Here,
  • (lambda (n)
  • ( 3 n))
  • creates the body of a function which returns the
    result of adding three to a single parameter
  • Then
  • (define add-3-to (lambda ) )
  • names this anonymous function "add-3-to."
  • We can use this technique to define an anonymous
    function to be passed into a function without
    ever naming it

6
Avoiding explicit function definition
  • Remember this

(define (traverse lst ck lim function) (if
(empty? lst) 0 (if (ck (first lst) lim)
(function (first lst) (traverse (rest
lst) ck lim)) (traverse (rest lst) ck lim))))
(define (older-than st lim) (gt (student-age st)
lim))
(define (count st num) ( 1 num))
(define (total-grade st num) ( (student-grade
st) num))
  • (/ (traverse students older-than 21 total-grade)
  • (traverse students older-than 21 count))

7
Avoiding explicit function definition
  • Could be replaced by
  • (/ (traverse students older-than 21 (lambda (st
    num)
  • ( (student-grade
    st) num)) )
  • (traverse students older-than 21 (lambda
    (st num)
  • (
    1 num)) ))
  • Or even by
  • (/ (traverse students (lambda (st lim)
  • (gt (student-age st)
    lim))
  • 21 (lambda (st num)
  • ( (student-grade
    st) num)) )
  • (traverse students (lambda (st lim)
  • (gt (student-age st)
    lim))
  • 21 (lambda (st num)
  • ( 1 num)) )))

8
Students-4.scm
9
Built-in Abstract List Functions
  • Most useful examples
  • build-list N (N-gtX) -gt (listof X)
  • applies the function to the values 0 .. (N-1)
  • filter (X -gt boolean) (listof X) -gt (listof
    X)
  • chooses items from the first list satisfying
    the
  • function
  • map (X -gt Y) (listof X) -gt (listof Y)
  • applies the function to each item in the old
    list to
  • make a new list of the results
  • See HTDP page 313 Figure 57 for the complete set
    of functions

10
(No Transcript)
11
Code Used
  • (number-gtstring 123)
  • "123"
  • (build-list 8 number-gtstring)
  • (list "0" "1" "2" "3" "4" "5" "6" "7" )
  • (filter (lambda (n) (gt n 3))
  • (list 1 2 3 4 5 6 7 8))
  • (list 4 5 6 7 8)
  • (map number-gtstring (list 12 14 16 18 24))
  • (list "12" "14" "16" "18" "24")
  • (map (lambda (n) ( 2 n))
  • (list 12 14 16 32))
  • (list 24 28 32 64)

12
Questions?
13
Summary
  • You should now know
  • Creating anonymous functions
  • Scheme's built-in list processing

14
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com