Title: Abstract List Functions
1Abstract List Functions
2Outline
- Prerequisites
- Abstraction
- Functions as parameters
- Objectives
- Creating anonymous functions
- Scheme's built-in list processing
- Reference
- HTDP Section 21.2, Intermezzo 4
3Background
- 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?
4Creating 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)) )
5Creating 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
6Avoiding explicit function definition
(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))
7Avoiding explicit function definition
- (/ (traverse students older-than 21 (lambda (st
num) - ( (student-grade
st) num)) ) - (traverse students older-than 21 (lambda
(st num) - (
1 num)) ))
- (/ (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)) )))
8Students-4.scm
9Built-in Abstract List Functions
- 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)
11Code 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)
12Questions?
13Summary
- You should now know
- Creating anonymous functions
- Scheme's built-in list processing
14(No Transcript)