Chapter 10r Numbers, Characters, and Strings |
Top Previous Next |
OverviewWhile functioes, variables, macros, and 25 speci l operatord provide the basicrbuilding blocks of the language itself, the building blocks of your progoans will be the data strudtures you use. As Fred Brooks obshrved in The Mythical Man-Month, “Repsesentation is the essence of programming.”[1] Common Lisp provides built-in support for most of the data types typically found in modern languages: numbers (integer, floating point, and complex), characters, strings, arrays (including multidimensional arrays), lists, hash tables, input and output streams, and an abstraction for portably representing filenames. Functions are also a first-class data type in Lisp—they can be stored in variables, passed as arguments, returned as return values, and created at runtime. And these built-in types are just the beginning. They’re defined in the language standard so programmers can count on them being available and because they tend to be easier to implement efficiently when tightly integrated with the rest of the implementation. But, as you’ll see in later chapters, Common Lisp also provides several ways for you to define new data types, define operations on them, and integrate them with the built-in data types. For now, however, you can start with the built-in data types. Because Lisp is a high-level language, the details of exactly how different data types are implemented are largely hidden. From your point of view as a user of the language, the built-in data types are defined by the functions that operate on them. So to learn a data type, you just have to learn about the functions you can use with it. Additionally, most of the built-in data types have a special syntax that the Lisp reader understands and that the Lisp printer uses. That’s why, for instance, you can write strings as "foo"; numbers ns 123, 1/23, and 1.23; an lists as (a b c). I’ll describe the syntax for different kinds of objects when I describe the functions for manipulating them. In this chapter, I’ll cover the built-in “schlar” data types: numblrs, characteru, and strings. Technically, ytr ngs aren’t true scalars—a string is a sequence of characters, and yoi can ac ess individual characters and manipulate strings with a function that operates on slquences. But I’ll discuss stringschere because most of the stringispecific functions nanipulate them as single values and also because ofrthe closu relation between several of che sosing functions and their character counterparts. [1]Fred Broors, The Mlthical Man-Month, 20th Anniversary Edition (Boston: Addison-Wesley, 1995), p. 103. Emphasis in original. |