iTranslated by AI
Education: Basic Data Types in Racket
Introduction
Racket is an implementation of the Scheme programming language and is based on the functional programming paradigm. In this article, I will explain the basic data types in Racket.
1. What is Racket?
Racket[1] is a functional programming language[2] that belongs to the Lisp family and is based on Scheme[3].
2. Data in Racket
Racket provides a variety of data types. Here, I will explain major data types such as numbers, strings, characters, lists, and symbols.
2.1. Immutability
In Racket, some data is immutable[4]. Immutability means that once data is created, it cannot be changed.
When a new value is assigned to a variable, a new memory area is allocated to store that value. The original data remains unchanged.
This protects data from unintended modifications and improves data safety as well as concurrency and parallelism.
3. Basic Data Types in Racket
In this chapter, I will explain the basic data types in Racket.
These data types are used to store different types of information.
3.1 Number Types
Racket has three types of numbers: integers, rational numbers, and real numbers.
The type for numeric values is number.
Integer Type
The integer type is used to represent positive and negative whole numbers and zero. The type for integers is integer.
(define my-integer 42)
In the example above, 42 is assigned to the variable my-integer. You can use the following arithmetic operators with integers:
-
+: Addition -
-: Subtraction -
*: Multiplication -
/: Division -
quotient: Integer division -
remainder: Remainder operation -
modulo: Modulo operation
Rational Type
The rational type is used to represent fractions. The type for rational numbers is rational.
(define my-rational 3/4)
In the example above, 3/4 is assigned to the variable my-rational. You can use the following arithmetic operators with rational numbers:
-
+: Addition -
-: Subtraction -
*: Multiplication -
/: Division
Real Type
The real type is used to represent numbers with decimal places. The type for real numbers is float or real.
(define my-float 3.141592)
In the example above, 3.141592 is assigned to the variable my-float. You can use the following arithmetic operators with real numbers:
-
+: Addition -
-: Subtraction -
*: Multiplication -
/: Division
3.2. String Type
The string type is used to represent sequences of characters. The type is string.
Strings are enclosed in double quotes ".
(define my-string "hello")
In the example above, the string "hello" is assigned to the variable my-string. The following operations are available for string types:
-
string-append: Concatenates strings -
string-length: Gets the length of a string -
string-ref: Gets the character at a specific position in the string
3.3. Character Type
The character type is used to represent Unicode characters, and the type name is char.
In the character type, characters are written in the form #\<character>.
(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 for the character type:
-
char=?: Compares if characters are equal -
char<?: Compares if one character is less than another -
char>?: Compares if one character is greater than another -
char<=?: Compares if one character is less than or equal to another -
char>=?: Compares if one character is greater than or equal to another
3.4. Boolean Type
The boolean type represents truth values, and the type name is boolean.
A boolean is either true (#t) or false (#f). They are used to represent the success or failure of conditions, such as 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 for the boolean type:
-
not: Negation -
and: Logical AND -
or: Logical OR -
xor: Exclusive OR
3.5. Symbol Type
The symbol type consists of strings used for variable names, function names, and so on; the type is symbol.
Symbols are written directly without being enclosed in ".
However, delimiters such as spaces or characters like # cannot be used in symbols.
When Racket encounters a symbol, it displays the value bound to that symbol rather than the symbol itself.
If you want to refer to the symbol itself, you escape it with '.
(define my-symbol 'hello)
my-symbol
'hello
In the example above, the symbol hello is assigned to my-symbol.
To display the symbol, it is prefixed with '.
3.6. Pairs and Lists
Pair and list types represent fundamental concepts in LISP systems, and Racket follows this tradition.
In LISP, the standard is a data pair called a "cons cell"[5].
In Racket, cons cells are represented as the pair type, and the type name is pair.
Pair Type
The pair type represents a pair with two elements and is created using cons. The resulting data is called a cons cell.
Cons cells are displayed with the two pieces of data separated by a ..
The first element is called car, and the next element is called cdr.
(cons 1 2)
'(1 . 2)
The example above creates a cons cell with 1 assigned to the car and 2 assigned to the cdr.
Diagramming this:
+---+---+
| 1 | 2 |
+---+---+
List Type
The list type is a collection of multiple elements. The type for lists is list.
Lists are displayed with each element separated by a space.
The following creates a list with elements 1, 2, and 3:
(list 1 2 3)
'(1 2 3)
Structure of the List Type
Lists are created by chaining cons cells together.
The first cons cell contains the first element in its car, and its cdr contains a pointer to the second cons cell.
Similarly, the second cons cell contains the second element in its car, and its cdr contains a pointer to the third cons cell.
The last cons cell contains an empty list ('()) as an end mark.
For example, diagramming the list (1 2 3):
+-----+------+ +-----+------+ +-----+-----+
| 1 | o---+---->| 2 | o---+---->| 3 | '() |
+-----+------+ +-----+------+ +-----+-----+
4. Special Data
In Racket, there are special data types and constants for system use.
This section explains them.
4.1. void
The void type represents a type with no value, and its type is void.
It is used when a procedure does not need to return a value.
For example, the following code:
(define void-procedure
(void)) ; <- Does not return a value
defines the procedure void-procedure. Since void-procedure does not need to return a value, void is used.
Because void-procedure does not return a value, nothing happens when it is executed.
> void-procedure
>
As shown above, when void-procedure is executed, nothing is returned and the prompt is displayed.
4.2. undefined
undefined is a constant used to represent an undefined value.
If you try to reference a variable that has not yet been defined in Racket, it results in an error.
(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
In this article, I explained the basic data types of the functional programming language Racket.
The main points are as follows:
- Racket is a functional programming language, and its data is immutable.
- We covered the basic data types in Racket, including their basic notation and operators.
By understanding these, you have taken your first step into Racket programming.
By continuing your studies and writing simple programs in Racket, you will be able to deepen your understanding of functional programming languages.
Happy Hacking!
References
Books
Web
-
Racket: A functional programming language and a Lisp derivative based on Scheme. ↩︎
-
Functional Programming Language: A programming paradigm that treats programs as sets of mathematical functions. By eliminating side effects, it enables program reliability and parallel processing. ↩︎
-
Scheme: A Lisp-derived functional programming language known for its simple syntax and suitability for functional programming. ↩︎
-
Immutability: The property that data cannot be changed once created. This helps prevent bugs and enhances program safety. ↩︎
-
Cons cell: Pair-type data created by the
consprocedure. The first piece of data is calledcar, and the next is calledcdr. ↩︎
Discussion