Lecture overview -- Keyboard shortcut: 'u'  Previous page: Linear search in lists -- Keyboard shortcut: 'p'  Next page: Mapping and filtering [Section] -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home    Lecture 4 - Page 5 : 34
Functional Programming in Scheme
Higher-order Functions
Generation of list selectors

It is attractive to generate generalizations of the list selector functions car, cadr, etc

(define (make-selector-function n)
  (lambda (lst) (list-ref lst (- n 1))))

A simple version of the make-selector-function function.

y:/Kurt/Files/courses/prog3/prog3-03/sources/notes/includes/make-selector-function.scmThe existing LAML make-selector-function.

It is crucial that you get good error messages in case you access a non-existing list component. This version handles this. For error messages purposes this version of the function accepts an optional parameter, which (somehow redundantly) gives the name of the selector function.

y:/Kurt/Files/courses/prog3/prog3-03/sources/notes/includes/make-selector-function-sessionExample usages of the function make-selector-function.

In interaction 1 through 3 we demonstrate generation and use of the first function. Next we outline how to define accessors of data structures, which are represented as lists. In reality, we are dealing with list-based record structures. In my every day programming, such list structures are quite common. It is therefore immensely important, to access data abstractly (via name accessors, instead of via the position in the list (car, cadr, etc). In this context, the make-selector-function comes in handy.