Go to the first, previous, next, last section, table of contents.

Higher-Order Procedures

Scheme is designed to make it easy to use higher-order procedures, i.e., procedures that may take other procedures as arguments or return them as values.

For example, you can easily write a sort procedure that takes a comparison procedure as an argument, and uses whatever procedure you hand it to determine the sorted order.

To sort a list in ascending order, you can then call sort with (a pointer to) the procedure < ("less than") as its argument, like this:

(sort < '(5 2 3))

and you'll get back a sorted list (2 3 5).

Note that the expression < here is just a variable reference. We're fetching the value of the variable < and passing it to sort as an argument.

If you'd rather sort the list in descending order, you can pass it the procedure > ("greater than") instead:

(sort > '(5 2 3))

and get back a sorted list (5 3 2).

The same procedure can be used with lists of different kinds of objects, as long as you supply a comparison operator that does what you want.

For example, to sort a list of character strings into alphabetic order, you can pass sort a pointer to the standard string-comparison procedure string<?,

(sort string<? '("foo" "bar" "baz" "quux"))

and get back a list ("bar" "baz" "foo" "quux").

[ give map example here? ]


Go to the first, previous, next, last section, table of contents.