iTranslated by AI
Basics of Symbols in Racket
Introduction
In this article, we will explain symbols in Racket in detail.
Symbols are basic elements used for variable names and function names in Racket programming, and they contribute to the readability and efficiency of programs.
1. Basic Concept of Symbols
In Racket programming, a symbol is used as a unique identifier (Identifier)[1] for variable names and function names.
Symbols are usually represented by a single word or string and are used as identifiers such as variable names and function names.
1.1 Immutability of Symbols
In Racket programming, a symbol is immutable[2] once defined.
This means that once a value is set, it cannot be changed.
This immutability is important for ensuring the stability of the program, guaranteeing that variables and functions do not undergo unexpected changes.
For example, the following code test.rkt:
(define x 10)
(writeln x) ; 10
(define x 20) ; Change the value of x to 20
(writeln x)
Running the above code will result in the following error:
test.rkt:5:8: module: identifier already defined
at: x
in: (define-values (x) 20)
As shown, you cannot overwrite the value of an already defined symbol x.
1.2 Scope
In Racket, symbols may have different values depending on their scope.
In code, it looks like this:
(define x 10)
(define y 20)
(writeln x) ; 10
(writeln y) ; 20
(let ([x 30]) ; Use the same symbol `x` in local scope
(writeln x)) ; 30
In this way, the second x displays 30.
This is because the value of x is shadowed within the let block.
2. Creating Symbols
Racket symbols can be created from any string. Not only alphanumeric characters but also characters from other languages can be used.
Additionally, symbols like - and * can also be used.
The following symbols are all perfectly valid:
one+two-
シンボル(Symbol) -its-a-symbol-
2.1. Case Sensitivity
Symbols are case-sensitive. The symbol Hello and the symbol hello are different symbols. Therefore, the result of the function eq?, which checks for object identity, will be false (#f).
In code, it looks like this:
(eq? 'Hello 'hello)
#f
2.2. Forbidden Characters
Symbols cannot contain spaces or special characters ((, ), [, ], {, }, ", ,, ', \``, ;, #, |, `).
Spaces act as delimiters. Therefore, a string containing a space cannot be used as a single symbol.
Special characters are used to represent data in Racket. To distinguish symbols from data, they cannot be used in symbols.
-
my symbol: (contains a space) -
a,b: (contains,)
Additionally, data types such as numbers or strings cannot be used as symbols.
-
"abc": (strings cannot be used as symbols)
Strings like the following can be used as symbols:
-
1a: (allowed even if it starts with a number) -
1+1: (allowed even with number + symbol)
2.3. Symbol Escaping
To handle symbols in Racket, you need to escape them. Escaping is done by adding ' to the beginning of the symbol.
Example:
hello → 'hello
Symbols can also be escaped by enclosing them in |.
In this case, you can create symbols that include special characters such as spaces.
For example, |hello world| becomes the symbol hello world.
In code, it looks like this:
(define x '|hello world|) ; Create the 'hello world' symbol
x
'|hello world| ; Symbols containing spaces are displayed enclosed in '|'
2.4. Creating Symbols Using Escape Characters
Using \ allows you to create symbols that include spaces or special characters.
For example, if you use hello\ world, a symbol named hello world will be created.
In code, it looks like this:
(define x 'hello\ world)
x
'|hello world|
2.5. Creating Symbols from Strings
The function string->symbol is a function that takes a string as an argument and returns a symbol.
You can use string->symbol to create a symbol from a string.
(string->symbol "hello world")
'|hello world|
2.6. Symbols and Identity (Interned)
Symbols in Racket are interned[3]. This means that symbols with the same name are guaranteed to be the same object.
Since they are the same object, the result of the eq? function returns #t.
In code, it looks like this:
(define x 'hello)
(define y (string->symbol "hello"))
(eq? x y) ; True because x and y are identical symbols
#t
Conclusion
In this article, we explained symbols in Racket.
Symbols are used for variables and functions in Racket programs and form the basis of Racket programming.
Use this article and the references to deepen your understanding of symbols and improve your Racket programming skills.
Happy Hacking!
References
Books
- How to Design Programs: <https://htdp.org/ >
Websites
- Racket Guide - Symbols: <https://docs.racket-lang.org/guide/symbols.html >
- Racket Reference - Symbols: <https://docs.racket-lang.org/reference/symbols.html >
Discussion