iTranslated by AI
Racket: Basic Data Types
Introduction
In this article, we will explain the basic data types of Racket in detail.
Racket is a functional programming language derived from LISP and is widely used for educational purposes.
1. What is Racket?
Racket is a language derived from Scheme, designed to make it easy for beginners to learn functional programming.
This language is widely used not only for education but also for research purposes.
2. Dynamic Data Types and Immutability (immutable)
Racket's data types have dynamic characteristics and immutability.
By using dynamic data types, data can be manipulated without type declarations, allowing developers to code more freely.
On the other hand, immutability maintains data consistency and reduces program bugs.
2.1 Dynamic Data Types
In Racket, data types are handled dynamically. This allows the program to flexibly change the type of data at runtime.
2.2 Immutability (immutable)
In Racket, data is basically immutable. This means that once data is created, it cannot be changed thereafter.
For example, if you try to redefine the value of variable x as in the following code, an error will occur.
(define x 10)
(println x) ; 10
(define x 20) ; change x to 20
(println x)
When executed, an identifier already defined error occurs as follows:
module: identifier already defined
at: x
If you want to overwrite a value, use set! as follows:
(define x 10)
(println x) ; 10
(set! x 20) ; change x to 20
(println x)
3. Basic Data Types in Racket
Various data types exist in Racket, but this article focuses on basic data types such as numeric, string, and boolean types.
3.1 Numeric Types
Racket has numeric types to represent numbers, and the type is number.
Numeric types are used to perform arithmetic operations.
Numeric types include integer, rational, and real types.
For example, you can perform simple arithmetic operations using integer types as follows:
(define my-integer 42)
(+ my-integer 8) ; 50
Special Numbers
The following symbols are defined as special numbers:
-
+inf.0: Positive infinity -
-inf.0: Negative infinity -
+nan.0/-nan.0: Not a Number (NaN)
3.2 Integer Types
Integer types are used to represent positive and negative integers and 0. The type for integers is integer.
(define my-integer 42)
In the above example, the variable my-integer is assigned the value 42. You can use the following arithmetic operators with integer types:
-
+: Addition -
-: Subtraction -
*: Multiplication - `/ : Division
-
quotient: Integer division -
remainder: Remainder operation -
modulo: Modulo operation
Integer types can also be written in binary, octal, and hexadecimal.
They are written with prefixes (#b, #o, #x) as shown below:
-
Binary:#b1011=11 -
Octal:#o15=13 -
Hexadecimal:#xa3=163
3.3 Rational Types
Rational types are used to represent fractions. The type for rational numbers is rational.
(define my-rational 3/4)
In the example above, the value 3/4 is assigned to the variable my-rational. You can use the following arithmetic operators with rational types:
-
+: Addition -
-: Subtraction -
*: Multiplication -
/: Division
3.4 Real Types
Real types are used to represent numbers with decimal places. The type for real numbers is float or real.
(define my-real 3.141592)
In the example above, the value 3.141592 is assigned to the variable my-real. You can use the following arithmetic operators with real types:
-
+: Addition -
-: Subtraction -
*: Multiplication -
/: Division
3.5 String Types
String types are used to represent character strings, and the type is string.
Strings are enclosed in ".
Various string operations such as creation, concatenation, and character access are possible with string types.
-
string-append: Concatenates strings -
string-length: Gets the length of a string -
string-ref: Gets the character at a specified position in a string
You can create messages by manipulating strings as shown below:
(define my-string "hello ")
(displayln (string-append my-string "world"))
3.6 Character Types
Character types are used to represent Unicode characters, and the type for character types is char.
In character types, characters are written in the form of #\<character>, such as #\f.
(define my-char #\あ)
my-char
#\あ
(char->integer my-char)
12354
In the example above, the character あ is assigned to the variable my-char. The following comparison operators can be used with character types:
-
char=?: Compares whether characters are equal -
char<?: Compares the order of characters -
char>?: Compares the order of characters -
char<=?: Compares the order of characters -
char>=?: Compares the order of characters
3.7 Boolean Type
Boolean type is a data type that represents truth values, and the type is boolean.
The boolean type takes either the value true (#t) or false (#f).
It is used to represent the truth of conditions, such as in numerical comparisons.
(define my-boolean #t)
In the example above, #t is assigned to the variable my-boolean.
The following logical operators can be used with boolean types:
-
not: Negation -
and: Logical product (AND) -
or: Logical sum (OR) -
xor: Exclusive OR
3.8 Symbol Type
Symbol type is a string of characters used for things like variable names and function names, and the type is symbol.
Symbols are written as they are without being enclosed in ".
On the other hand, whitespace and special characters (e.g., #) cannot be used as symbols.
Racket displays the value bound to a symbol when it finds one.
If you want to write the symbol itself, you escape the symbol with '.
(define my-symbol 'hello)
my-symbol
'hello
In the example above, the symbol hello is assigned to my-symbol.
To indicate it is a symbol, it is displayed with a leading '.
3.9 Pair Type
Pair type is a data type representing the cons-cell, which is a fundamental data structure in LISP.
A cons-cell consists of a pair of two data items; the first element is called car, and the second is called cdr.
In Racket, they are represented by separating the two data items with a ..
(cons 1 2)
'(1 . 2)
The example above creates a cons-cell where 1 is assigned to car and 2 is assigned to cdr.
Illustrated diagrammatically:
+---+---+
| 1 | 2 |
+---+---+
3.10 List Type
List type is a collection of multiple elements grouped together. The type for lists is list.
A list displays each element separated by spaces.
The following creates a list with elements 1, 2, and 3:
(list 1 2 3)
'(1 2 3)
Structure of the List Type
A list is formed by connecting multiple cons-cells. The car part of each cons-cell holds the data for an element of the list, while the cdr part holds a pointer to the next cons-cell.
In the final cons-cell, the empty list '() is placed in the cdr part.
For example, illustrating the list (1 2 3):
+-----+------+ +-----+------+ +-----+-----+
| 1 | o---+---->| 2 | o---+---->| 3 | '() |
+-----+------+ +-----+------+ +-----+-----+
4. Special Data Types
In Racket, there are special data types and constants used by the system.
This section explains them.
4.1 void
The void type is a special type used for procedures that do not require a return value.
For example, it is used in functions intended only for side effects.
Using this type does not affect the program's output and only performs internal state changes.
The following code defines a procedure void-procedure that does not return a value, using void because a return value is not needed:
(define void-procedure
(void)) ; ← Does not return a value
The execution result of void-procedure is as follows:
> void-procedure
>
Since void-procedure does not return a value, the prompt is displayed.
4.2 undefined
undefined is a constant used to represent an undefined value.
In Racket, attempting to reference an undefined variable results in undefined, and an error occurs.
(define y 1)
(display (+ x y)) ; Error because x is undefined
x: undefined;
cannot reference an identifier before its definition
in module: top-level
[,bt for context]
Conclusion
This concludes the explanation of the basic data types in Racket.
Next, let's move on to learning about syntax, conditional branching, and function definitions in Racket.
Learning programming with Racket will surely deepen your understanding of functional programming.
Happy Hacking!
Technical Terms and Annotations
Here are the technical terms and their annotations.
-
Racket:
A functional programming language based on the Scheme language, suitable for language theory and experimental projects. -
Dynamic Data Type:
A language characteristic where the data type is determined at runtime. -
Immutability(immutable):
The property where an object's state cannot be changed once it has been created. -
Pair Type:
A data structure containing two values. -
List Type:
A data type that stores multiple elements as a sequence of linked lists. -
cons-cell:
A data structure consisting of two elements,carandcdr.
Discussion