Chapter 6: Operations on Objects


click to expand

Three two-armed spirals.

This chapter describes the operations on objects, including lists, numbers, characters, strings, vectors, and symbols. The first section covers constant objects and quotation. The second section describes generic equivalence predicates for comparing two objects and predicates for determining the type of an object. Later sections describe procedures that deal primarily with one of the object types mentioned above. There is no section treating operations on procedures, since the only operation defined specifically for procedures is application, and this is described in Chapter 5. Operations on ports are covered in the more general discussion of input and output in Chapter 7.

6.1. Constants and Quotation

constant

syntax

returns: constant

constant is any self-evaluating constant, i.e., a number, boolean, character, or string. Constants are immutable; see the note in the description of quote below.

 3.2  3.2 #f  #f #\c  #\c "hi"  "hi" 

(quote obj )

syntax

'obj

syntax

returns: obj

'obj is equivalent to (quote obj ). The abbreviated form is converted into the longer form by the Scheme reader (see read).

quote inhibits the normal evaluation rule for obj, allowing obj to be employed as data. Although any Scheme object may be quoted, quotation is not necessary for self-evaluating constants, i.e., numbers, booleans, characters, and strings.

Quoted and self-evaluating constants are immutable. That is, it is an error to alter a constant via set-car!, string-set!, etc. An implementation may choose to share storage among different constants to save space.

 (+ 2 3)  5 '(+ 2 3)  (+ 2 3) (quote (+ 2 3))  (+ 2 3) 'a  a 'cons  cons '()  () '7  7 

(quasiquote obj )

syntax

'obj

syntax

(unquote obj )

syntax

,obj

syntax

(unquote-splicing obj )

syntax

,@obj

syntax

returns: see explanation

obj is equivalent to (quasiquote obj ), ,obj is equivalent to (unquote obj ), and, @obj is equivalent to (unquote-splicing obj ). The abbreviated forms are converted into the longer forms by the Scheme reader (see read).

quasiquote is similar to quote, but it allows parts of the quoted text to be "unquoted." Within a quasiquote expression, subforms of unquote and unquote-splicing forms are evaluated, and everything else is quoted, i.e., left unevaluated. The value of each unquote subform is inserted into the output in place of the unquote form, while the value of each unquote-splicing subform is spliced into the surrounding list or vector structure. unquote and unquote-splicing are valid only within quasiquote expressions.

quasiquote expressions may be nested, with each quasiquote introducing a new level of quotation and each unquote or unquote-splicing taking away a level of quotation. An expression nested within n quasiquote expressions must be within n unquote or unquote-splicing expressions to be evaluated.

 '(+ 2 3)  (+ 2 3) '(+ 2 ,(* 3 4))  (+ 2 12) '(a b (,(+ 2 3) c) d)  (a b (5 c) d) '(a b ,(reverse '(c d e)) f g)  (a b (e d c) f g) (let ((a 1) (b 2))   '(,a . ,b))  (1 . 2) '(+ ,@(cdr '(* 2 3)))  (+ 2 3) '(a b ,@(reverse '(c d e)) f g)  (a b e d c f g) (let ((a 1) (b 2))   '(,a ,@b))  (1 . 2) '#(,@(list 1 2 3))  #(1 2 3) ",(cons 'a 'b)  ',(cons 'a 'b) ",(cons 'a 'b)  '(a . b) 




The Scheme Programming Language
The Scheme Programming Language
ISBN: 026251298X
EAN: 2147483647
Year: 2003
Pages: 98

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net