iTranslated by AI
Input and Output in Racket
Introduction
In this article, I will explain the basic input and output functions of the functional programming language Racket using specific sample programs.
This will enable you to implement I/O in your Racket programs and apply it to real-world projects.
Here, I introduce the main I/O functions in Racket and explain their features and usage examples.
For more details, please refer to the Racket Reference to explore other functions and usage examples.
1. Ports and Standard I/O
Racket's input and output functions can be used by specifying a port.
A port is an important Racket object that uniformly manages input and output for files, networks, and more.
Racket's I/O functions input and output data through ports.
When no port is specified, the standard input/output ports are used.
The read function reads data from standard input if no port is specified.
The write, print, and display functions output data to standard output if no port is specified.
Racket programs using standard I/O functions (without specifying a port) support console input, pipes, and redirection.
This allows users to manipulate program I/O flexibly.
2. Input Functions
2.1 Details of the read Function
The read function interprets the input data as a Racket data type and returns it.
An error occurs if the input data is not valid as Racket data.
For example, an error occurs if a list's ( is closed with ] instead of ).
The following program reads input from the user and outputs that input along with its data type.
The program ends when EOF (End of File) (the Ctrl+Z key on Windows) is entered.
Executing this program results in the following:
> racket .\echo-loop.rkt
--- echo ---
input: abc
output: 'abc - symbol
input: 42
output: 42 - number
input: "hello"
output: "hello" - string
input: #\k
output: #\k - character
input: [1 . 2]
output: '(1 . 2) - pair
input: '(1 2 3)
output: ''(1 2 3) - list
input: ^Z
output: #<eof> - unknown
2.2 Input from a File
To read data from a file, use a file port.
An example of file input is as follows:
(define fin (open-input-file "data.txt"))
(define data (read fin))
(printf "read data: ~a\n" data)
(close-input-port fin)
2.3 Character Input
In addition to the read function, there are the read-char function for character input and the read-line function for line input.
The read-char function reads one character at a time from standard input, and the read-line function reads one line up to the newline.
In this process, characters are read using UTF-8 encoding.
For more details, please refer to the Racket Reference.
3. Output Functions
3.1 Differences between write, print, and display
The write, print, and display functions have different roles.
Their respective roles are as follows:
-
write:
Outputs data read byreadin its original format. -
print:
Outputs data as it appears in theREPL. Strings, symbols, etc., are quoted. -
display:
Outputs data intended for screen display. String, symbol, and character types are not quoted.
Refer to the following for output examples:
write |
print |
display |
|
|---|---|---|---|
| 1/2 (number) | 1/2 | 1/2 | 1/2 |
| #\x (character) | #\x | #\x | x |
| "hello" (string) | "hello" | "hello" | hello |
| '|tea pot| (symbol) | '|tea pot| | '|tea pot| | tea pot |
| '("I" pod) (list) | '("I" pod) | '("I" pod) | I pod |
| write (procedure) | #<procedure:write> | #<procedure:write> | #<procedure:write> |
3.2 Output to a File
By specifying a file port, data is output to a file.
For file output, open-output-file is used.
If the file already exists, the open-output-file function raises an exception, but this can be avoided by specifying the #:exists 'truncate option.
In this case, Racket erases all existing contents of the file and overwrites it with new content.
Instead of 'truncate, #:exist 'can-update can be specified.
In this case, the file content is not erased, but overwritten with new content.
The program is as follows:
(define fout (open-output-file "data.txt")) ; cause exception if file is already exist
(display "howdy" fout)
(close-output-port fout)
If the file already exists, an error like the following occurs, but it can be avoided with the #:exist 'truncate option:
> racket fileout.rkt
open-output-file: file exists
path: data.txt
context...:
body of
"fileout.rkt"
To avoid the above error, specify #:exists 'truncate:
(define fout (open-output-file "data.txt" #:exist 'truncate))
(display "howdy" fout)
(close-output-port fout)
3.3 Character Output
Corresponding to read-char, there is a write-char function for single-character output.
The write-char function outputs a single character read by read-char.
The read-line function is output using the regular write function.
Conclusion
In this article, I explained the basics of Racket's input and output functions.
By making full use of these functions, you can create practical programs that handle data input from external sources.
Let's deepen your learning of Racket, improve your functional programming skills, and strive to write more sophisticated programs.
Well then, Happy Hacking!
Technical Terms and Annotations
The following is a list of technical terms and their annotations.
-
readfunction:
A function that interprets input data asRacketdata and returns it as typed data. -
writefunction:
A function that outputsRacketdata types in their original format. -
printfunction:
A function that outputsRacketdata in the format used by theREPL. -
displayfunction:
A function that outputs data in a simple format without quotes. -
Port:
ARacketobject for inputting and outputting data through functions. -
File port:
A port for performing input and output to files.
References
Websites
-
Racket Guide:
The I/O chapter of theRacket Guide -
Racket Reference:
The I/O chapter of theRacket Reference
Discussion