2.2.4 Conversions between data types
Scheme offers many procedures for converting among
the data types. We already know how to convert between
the character cases using
(char->integer #\d) => 100 (integer->char 50) => #\2 Strings can be converted into the corresponding list of characters.
(string->list "hello") => (#\h #\e #\l #\l #\o)
Other conversion procedures in the same vein are
Numbers can be converted to strings:
(number->string 16) => "16"
Strings can be converted to numbers. If the string
corresponds to no number,
(string->number "16") => 16 (string->number "Am I a hot number?") => #f
(string->number "16" 8) => 14
because Symbols can be converted to strings, and vice versa:
(symbol->string 'symbol) => "symbol" (string->symbol "string") => string 2.2.2 VectorsVectors are sequences like strings, but their elements can be anything, not just characters. Indeed, the elements can be vectors themselves, which is a good way to generate multidimensional vectors. Here's a way to create a vector of the first five integers:
(vector 0 1 2 3 4) => #(0 1 2 3 4)
Note Scheme's representation of a vector value: a
In analogy with
(define v (make-vector 5))
The procedures 2.2.1 StringsStrings are sequences of characters (not to be confused with symbols, which are simple data that have a sequence of characters as their name). You can specify strings by enclosing the constituent characters in double-quotes. Strings evaluate to themselves.
"Hello, World!" => "Hello, World!"
The procedure
(string #\h #\e #\l #\l #\o) => "hello"
Let us now define a global variable
(define greeting "Hello; Hello!") Note that a semicolon inside a string datum does not trigger a comment.
The characters in a given string can be individually
accessed and modified. The procedure
(string-ref greeting 0) => #\H New strings can be created by appending other strings:
(string-append "E " "Pluribus " "Unum") => "E Pluribus Unum" You can make a string of a specified length, and fill it with the desired characters later.
(define a-3-char-long-string (make-string 3))
The predicate for checking stringness is
Strings obtained as a result of calls to
(define hello (string #\H #\e #\l #\l #\o)) hello => "Hello" (string-set! hello 1 #\a) hello => "Hallo" 2.1.3 Characters
Scheme character data are represented by prefixing the
character with
The character predicate is
(char? #\c) => #t (char? 1) => #f (char? #\;) => #t Note that a semicolon character datum does not trigger a comment.
The character data type has its set of comparison
predicates:
(char=? #\a #\a) => #t (char<? #\a #\b) => #t (char>=? #\a #\b) => #f
To make the comparisons case-insensitive, use
(char-ci=? #\a #\A) => #t (char-ci<? #\a #\B) => #t
The case conversion procedures are
(char-downcase #\A) => #\a (char-upcase #\a) => #\A 5.10.2 Local Variable BindingsAs opposed to definitions at the top level, which are visible in the whole program (or current module, when Guile modules are used), it is also possible to define variables which are only visible in a well-defined part of the program. Normally, this part of a program will be a procedure or a subexpression of a procedure. With the constructs for local binding ( The first local binding construct is
— syntax: let bindings body
— syntax: let* bindings body
— syntax: letrec bindings body
There is also an alternative form of the 3.1.2.3 Creating and Using a New ProcedureScheme has lots of standard procedures, and Guile provides all of these via predefined top level variables. All of these standard procedures are documented in the later chapters of this reference manual. Before very long, though, you will want to create new procedures that
encapsulate aspects of your own applications' functionality. To do
this, you can use the famous For example, the value of the following Scheme expression (lambda (name address) expression ...) is a newly created procedure that takes two arguments:
To make things more concrete, let's suppose that the two arguments are both strings, and that the purpose of this procedure is to form a combined string that includes these arguments. Then the full lambda expression might look like this: (lambda (name address)
(string-append "Name=" name ":Address=" address))
We noted in the previous subsection that the procedure part of a procedure invocation expression can be any Scheme expression whose value is a procedure. But that's exactly what a lambda expression is! So we can use a lambda expression directly in a procedure invocation, like this: ((lambda (name address)
(string-append "Name=" name ":Address=" address))
"FSF"
"Cambridge")
This is a valid procedure invocation expression, and its result is the
string It is more common, though, to store the procedure value in a variable — (define make-combined-string
(lambda (name address)
(string-append "Name=" name ":Address=" address)))
— and then to use the variable name in the procedure invocation: (make-combined-string "FSF" "Cambridge") Which has exactly the same result. 3.1.2.2 Simple Procedure InvocationA procedure invocation in Scheme is written like this: (procedure [arg1 [arg2 ...]]) In this expression, procedure can be any Scheme expression whose value is a procedure. Most commonly, however, procedure is simply the name of a variable whose value is a procedure. For example, (string-append "/home" "/" "andrew") is a procedure invocation whose result is the string value
Similarly, (string-length "abc") is a procedure invocation whose result is the numeric value 3. Each of the parameters in a procedure invocation can itself be any Scheme expression. Since a procedure invocation is itself a type of expression, we can put these two examples together to get (string-length (string-append "/home" "/" "andrew")) — a procedure invocation whose result is the numeric value 12. (You may be wondering what happens if the two examples are combined the other way round. If we do this, we can make a procedure invocation expression that is syntactically correct: (string-append "/home" (string-length "abc")) but when this expression is executed, it will cause an error, because
the result of 3.1.1.3 Defining and Setting VariablesTo define a new variable, you use Scheme's (define variable-name value) This makes a new variable called variable-name and stores value in it as the variable's initial value. For example: ;; Make a variable `x' with initial numeric value 1.
(define x 1)
;; Make a variable `organization' with an initial string value.
(define organization "Free Software Foundation")
(In Scheme, a semicolon marks the beginning of a comment that continues
until the end of the line. So the lines beginning Changing the value of an already existing variable is very similar,
except that (set! variable-name new-value) Remember that variables do not have fixed types, so new-value may have a completely different type from whatever was previously stored in the location named by variable-name. Both of the following examples are therefore correct. ;; Change the value of `x' to 5.
(set! x 5)
;; Change the value of `organization' to the FSF's street number.
(set! organization 545)
|