5.6 Functions on Lists

Armed with an understanding of (1) the core computational model in Lisp— λ-calculus; (2) the recursive specifications of data structures and recursive definitions of algorithms; and (3) the representation of lists in memory, we are prepared to develop functions that operate on data structures.

5.6.1 A List length Function

Consider the following function length1,8 which given a list, returns the length of the list:

A set of five code lines in Scheme with the function length 1.
Description

The built-in Scheme predicate null? returns true if its argument is an empty list and false otherwise. The built-in Scheme predicate empty? can be used for this purpose as well.

Notice that the ...

Get Programming Languages: Concepts and Implementation now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.