1. Higher-Order functions
A higher-order function is a name given to any function that does at least one of the following
-
One or more of its arguments is a function.
-
Returns a function as a result.
Here are the signatures of some of the higher-order functions provided by DrRacket.
;;;; Signature
;; map : [X -> Y] List<X> -> List<Y>
;;;; Purpose
;; GIVEN: a function and a list
;; RETURNS: a list that holds the results of applying the function
;; to each element of the input list.
;;;; Signature
;; andmap: [X -> Boolean] List<X> -> Boolean
;;;; Purpose
;; GIVEN: a predicate from some kind of data X to a boolean and
;; a list of X
;; RETURNS: the result of logically and-ing all results from applying
;; the function to each list element.
;;;; Signature
;; ormap: [X -> Boolean] List<X> -> Boolean
;;;; Purpose
;; GIVEN: a predicate from some kind of data X to a boolean and
;; a list of X
;; RETURNS: the result of logically or-ing all results from applying
;; the function to each list element.
;;;; Signature
;; foldr: [X Y -> Y] Y List<X> -> Y
;;;; Purpose
;; GIVEN: a function, a value for empty and a list of elements
;; RETURNS: the result or applying the cumulative result of applying f
;; the list elements from the last to the first
;;;; Signature
;; foldl: [X Y -> Y] Y List<X> -> Y
;;;; Purpose
;; GIVEN: a function, a value for empty and a list of elements
;; RETURNS: the result or applying the cumulative result of applying f
;; the list elements from the last to the first
;;;; Signature
;; filter: [X -> Boolean] LoF<X> -> LoF<X>
;;;; Purpose
;; GIVEN: a predicate function and a list of items
;; RETURNS: all elements of the list for which the function returns #true
2. Concrete Signatures for Higher-Order Functions
When we use a one of the predefined higher-order functions we are providing concrete data definitions (i.e., non-abstract). For example
> (map add1 (list 1 2 3 4))
(list 2 3 4 5)
we are using map
with the following concrete signature
> (map add1 (list 1 2 3 4)) ;; [Number -> Number] List<Number> -> List<Number>
(list 2 3 4 5)
3. Lists of Lists
One of your friends is finding it difficult to work with lists of lists and higher-order functions (HOF). His TA gave him same extra simple questions to help them practise and your friend is sharing these questions with you.
4. Managing a class of students
One of the professors in the department is asking for your help in developing programs to help them manage their class.
The would like to keep a list of all students in their class. For each student we would like to capture
-
the student’s name
-
the student’s id
-
a list of all the students homework grades