2.2.1 data type conversion
Scheme provides many data type conversion processes. We already know that we can use the char-downcase and char-upcase processes to convert uppercase and lowercase characters. The character can also be converted to an integer by using char-> integer. The same integer can also be converted to a character through integer-> char. (The result of converting a character to an integer is usually the ASCII value of the character .)
(char->integer#\d) => 100
(integer->char50) => #\2
A string can be converted to an equivalent character list.
(string->list"hello") => (#\h#\e#\l#\l#\o)
Other conversion processes share the same style: List-> string, vector-> list and list-> vector.
Numbers can be converted to strings: (number-> string16)=>"16"
A string can also be converted to a number. If the string cannot be converted to a number, # F is returned.
(string->number"16")
=> 16
(string->number"Am I a not number?")
=> #f
String-> Number the second parameter is an optional parameter, indicating the number of hexadecimal conversion.
(string->number"16"8) => 14
The octal value 16 equals 14.
Symbols can also be converted to a string, and vice versa:
(symbol->string'symbol)
=> "symbol"
(string->symbol"string")
=> string
2.1 other data types
Scheme also contains some other data types. One is procedure ). We have seen many processes, such as display, ++, and cons. In fact, they are some variables that carry process values. The internal values and characters of the process are not visible:
cons
=> <procedure>
All the processes we have seen so far belong to the original process (system process), which is carried by some global variables. You can also add custom processes.
Another data type isPorTPort. A port provides an execution channel for the input and output. Ports are usually associated with files and console operations.
In our "Hello, world !" Program, we use the display process to output a string to the console. Display can accept two parameters. The first parameter value is the output value, and the other value indicates the output port (port) that will host the display result ).
In our program, the second parameter of display is an implicit parameter. At this time, display uses the standard output port as its default output port. We can call the current-output-port process to obtain the current standard output port. We can make it clearer.
(display"Hello, World!" (current-output-port))
2.2 S-expressions
All these discussed data types can be unified into a commonS-expression(Symbol expression or S-expression) Data Type (s represents the symbol ). Like 42, # \ c,(
1 .
2)
,#(
AB
C)
, "Hello ",(
Quote
XYZ)
,(
String-> Number
"16")
, And(
Begin (
Display
"Hello,
World! ") (
Newline))
All are S-expressions.