Directories in F#
Here’s the translation of the Go code to F# with explanations in Markdown format suitable for Hugo:
Our first program will demonstrate how to work with directories in F#. We’ll use the System.IO
namespace, which provides various functions for file and directory operations.
open System
open System.IO
// Helper function to check for errors
let check (result: Result<unit, exn>) =
match result with
| Ok _ -> ()
| Error e -> raise e
// Helper function to create an empty file
let createEmptyFile (name: string) =
File.WriteAllText(name, "")
// Main function
let main() =
// Create a new sub-directory in the current working directory
Directory.CreateDirectory("subdir") |> ignore
// When creating temporary directories, it's good practice to ensure their removal
// We'll use a try-finally block to ensure cleanup
try
// Create files and directories
createEmptyFile "subdir/file1"
// Create a hierarchy of directories
Directory.CreateDirectory("subdir/parent/child") |> ignore
createEmptyFile "subdir/parent/file2"
createEmptyFile "subdir/parent/file3"
createEmptyFile "subdir/parent/child/file4"
// List directory contents
let parentContents = Directory.GetFileSystemEntries("subdir/parent")
printfn "Listing subdir/parent"
for entry in parentContents do
let isDirectory = Directory.Exists(entry)
printfn " %s %b" (Path.GetFileName(entry)) isDirectory
// Change the current working directory
Directory.SetCurrentDirectory("subdir/parent/child")
// List contents of the current directory
let currentContents = Directory.GetFileSystemEntries(".")
printfn "Listing subdir/parent/child"
for entry in currentContents do
let isDirectory = Directory.Exists(entry)
printfn " %s %b" (Path.GetFileName(entry)) isDirectory
// Change back to the original directory
Directory.SetCurrentDirectory("../../..")
// Recursively visit all files and directories
let rec visitDir (dir: string) =
printfn " %s %b" dir true
for entry in Directory.GetFileSystemEntries(dir) do
if Directory.Exists(entry) then
visitDir entry
else
printfn " %s %b" entry false
printfn "Visiting subdir"
visitDir "subdir"
finally
// Clean up: remove the created directory and all its contents
Directory.Delete("subdir", true)
// Run the main function
main()
To run the program, save it as Directories.fs
and use the F# compiler:
$ fsharpc Directories.fs
$ mono Directories.exe
Listing subdir/parent
child True
file2 False
file3 False
Listing subdir/parent/child
file4 False
Visiting subdir
subdir True
subdir/file1 False
subdir/parent True
subdir/parent/child True
subdir/parent/child/file4 False
subdir/parent/file2 False
subdir/parent/file3 False
This F# program demonstrates various directory operations:
- Creating directories and files
- Listing directory contents
- Changing the current working directory
- Recursively visiting a directory structure
The program uses functions from the System.IO
namespace to perform these operations. Note that F# uses a different approach for error handling and resource management compared to Go. We use a try-finally block to ensure cleanup, and pattern matching for error checking.
The visitDir
function demonstrates a recursive approach to directory traversal, which is equivalent to Go’s filepath.WalkDir
.
Remember to handle exceptions and clean up resources properly when working with file systems in real-world applications.