Directories in Scheme
Here’s the translation of the Go code to Scheme, formatted in Markdown suitable for Hugo:
Our first example demonstrates how to work with directories in Scheme. We’ll use the SRFI-170 POSIX API for file system operations.
(import (scheme base)
(srfi 170))
;; Helper function to create an empty file
(define (create-empty-file name)
(call-with-output-file name
(lambda (port)
(display "" port))))
;; Main function
(define (main)
;; Create a new sub-directory in the current working directory
(mkdir "subdir" #o755)
;; When creating temporary directories, it's good practice to ensure
;; their removal. In Scheme, we can use dynamic-wind for this.
(dynamic-wind
(lambda () #f) ; No setup needed
(lambda ()
;; Create files and directories
(create-empty-file "subdir/file1")
(mkdir-p "subdir/parent/child" #o755)
(create-empty-file "subdir/parent/file2")
(create-empty-file "subdir/parent/file3")
(create-empty-file "subdir/parent/child/file4")
;; List directory contents
(display "Listing subdir/parent\n")
(for-each
(lambda (entry)
(display " ")
(display (cdr entry))
(display " ")
(display (file-is-directory? (string-append "subdir/parent/" (cdr entry))))
(newline))
(directory-files "subdir/parent"))
;; Change current working directory
(chdir "subdir/parent/child")
;; List contents of current directory
(display "Listing subdir/parent/child\n")
(for-each
(lambda (entry)
(display " ")
(display (cdr entry))
(display " ")
(display (file-is-directory? (cdr entry)))
(newline))
(directory-files "."))
;; Change back to original directory
(chdir "../../.."))
(lambda ()
;; Cleanup: remove the created directory and its contents
(system "rm -rf subdir"))))
;; Run the main function
(main)
This Scheme program demonstrates various directory operations:
We start by importing necessary modules, including SRFI-170 for POSIX file operations.
We define a helper function
create-empty-file
to create empty files.In the
main
function, we perform several operations:- Create a new directory using
mkdir
. - Use
dynamic-wind
to ensure cleanup after operations. - Create files and nested directories.
- List directory contents using
directory-files
. - Change the current working directory with
chdir
. - Finally, clean up by removing the created directory structure.
- Create a new directory using
We use
for-each
to iterate over directory entries and display their names and whether they are directories.The cleanup is done using a system call to
rm -rf
, as Scheme doesn’t have a built-in recursive directory removal function.
Note that Scheme doesn’t have an exact equivalent to Go’s filepath.WalkDir
. To achieve similar functionality, you would need to implement a recursive directory traversal function manually.
To run this program, save it to a file (e.g., directories.scm
) and execute it using your Scheme interpreter. The output will show the created directory structure and its contents.
Remember that the exact behavior might vary depending on the Scheme implementation and the specific SRFI-170 support available.