Chapter 2

Chapter 2 - Teach Yourself Scheme in Fixnum Days [Go to...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Teach Yourself Scheme in Fixnum Days [Go to first , previous , next page; contents ; index ] Chapter 2 Data types A data type is a collection of related values. These collections need not be disjoint, and they are often hierarchical. Scheme has a rich set of data types: some are simple (indivisible) data types and others are compound data types made by combining other data types. 2.1 Simple data types The simple data types of Scheme include booleans, numbers, characters, and symbols. 2.1.1 Booleans Scheme's booleans are #t for true and #f for false. Scheme has a predicate procedure called boolean? that checks if its argument is boolean. (boolean? #t) => #t (boolean? "Hello, World!") => #f The procedure not negates its argument, considered as a boolean. (not #f) => #t (not #t) => #f (not "Hello, World!") => #f The last expression illustrates a Scheme convenience: In a context that requires a boolean, Scheme will treat any value that is not #f as a true value. 2.1.2 Numbers Scheme numbers can be integers (eg, 42 ), rationals ( 22/7 ), reals ( 3.1416 ), or complex ( 2+3i ). An integer is a rational is a real is a complex number is a number. Predicates exist for testing the various kinds of numberness: file:///C|/Documents%20and%20Settings/Linda%20Graue. ..tes/Teach%20Yourself%20Scheme/t-y-scheme-Z-H-4.html (1 of 12) [2/6/2008 11:38:43 AM]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Teach Yourself Scheme in Fixnum Days (number? 42) => #t (number? #t) => #f (complex? 2+3i) => #t (real? 2+3i) => #f (real? 3.1416) => #t (real? 22/7) => #t (real? 42) => #t (rational? 2+3i) => #f (rational? 3.1416) => #t (rational? 22/7) => #t (integer? 22/7) => #f (integer? 42) => #t Scheme integers need not be specified in decimal (base 10) format. They can be specified in binary by prefixing the numeral with #b . Thus #b1100 is the number twelve. The octal prefix is #o and the hex prefix is #x . (The optional decimal prefix is #d .) Numbers can tested for equality using the general-purpose equality predicate eqv? . (eqv? 42 42) => #t (eqv? 42 #f) => #f (eqv? 42 42.0) => #f However, if you know that the arguments to be compared are numbers, the special number-equality predicate = is more apt. (= 42 42) => #t (= 42 #f) -->ERROR!!! (= 42 42.0) => #t Other number comparisons allowed are < , <= , > , >= . (< 3 2) => #f (>= 4.5 3) => #t Arithmetic procedures + , - , * , / , expt have the expected behavior: (+ 1 2 3) => 6 (- 5.3 2) => 3.3 (- 5 2 1) => 2 file:///C|/Documents%20and%20Settings/Linda%20Graue. ..tes/Teach%20Yourself%20Scheme/t-y-scheme-Z-H-4.html (2 of 12) [2/6/2008 11:38:43 AM]
Background image of page 2
Teach Yourself Scheme in Fixnum Days (* 1 2 3) => 6 (/ 6 3) => 2 (/ 22 7) => 22/7 (expt 2 3) => 8 (expt 4 1/2) => 2.0 For a single argument, - and / return the negation and the reciprocal respectively: (- 4) => -4 (/ 4) => 1/4 The procedures max and min return the maximum and minimum respectively of the number arguments supplied to them. Any number of arguments can be so supplied. (max 1 3 4 2 3)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

Chapter 2 - Teach Yourself Scheme in Fixnum Days [Go to...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online