Directories in OCaml

Here’s the translation of the Go code to OCaml, with explanations in Markdown format suitable for Hugo:

Our program demonstrates how to work with directories in OCaml. Here’s the full source code:

open Unix
open Sys

(* Helper function to check for errors *)
let check_error result = 
  match result with
  | Ok _ -> ()
  | Error e -> failwith (Unix.error_message e)

(* Helper function to create an empty file *)
let create_empty_file name =
  let oc = open_out name in
  close_out oc

let () =
  (* Create a new sub-directory in the current working directory *)
  check_error (mkdir "subdir" 0o755);

  (* When creating temporary directories, it's good practice to remove them afterwards *)
  at_exit (fun () -> Unix.rmdir "subdir");

  create_empty_file "subdir/file1";

  (* We can create a hierarchy of directories, including parents *)
  check_error (mkdir "subdir/parent/child" 0o755);

  create_empty_file "subdir/parent/file2";
  create_empty_file "subdir/parent/file3";
  create_empty_file "subdir/parent/child/file4";

  (* List directory contents *)
  let entries = readdir "subdir/parent" in
  print_endline "Listing subdir/parent";
  Array.iter (fun entry ->
    let is_dir = (stat ("subdir/parent/" ^ entry)).st_kind = S_DIR in
    Printf.printf " %s %b\n" entry is_dir
  ) entries;

  (* Change the current working directory *)
  chdir "subdir/parent/child";

  (* Now we'll see the contents of subdir/parent/child when listing the current directory *)
  let entries = readdir "." in
  print_endline "Listing subdir/parent/child";
  Array.iter (fun entry ->
    let is_dir = (stat entry).st_kind = S_DIR in
    Printf.printf " %s %b\n" entry is_dir
  ) entries;

  (* Change back to where we started *)
  chdir "../../..";

  (* We can also visit a directory recursively, including all its sub-directories *)
  print_endline "Visiting subdir";
  let rec visit path =
    let entries = readdir path in
    Array.iter (fun entry ->
      if entry <> "." && entry <> ".." then
        let full_path = Filename.concat path entry in
        let is_dir = (stat full_path).st_kind = S_DIR in
        Printf.printf " %s %b\n" full_path is_dir;
        if is_dir then visit full_path
    ) entries
  in
  visit "subdir"

To run the program, save it as directories.ml and use ocamlc to compile and run:

$ ocamlc unix.cma directories.ml -o directories
$ ./directories
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 OCaml program demonstrates various operations on directories:

  1. We create directories using mkdir.
  2. We use at_exit to ensure cleanup of temporary directories.
  3. We list directory contents using readdir.
  4. We change the current working directory with chdir.
  5. We recursively visit a directory structure with a custom visit function.

Note that OCaml’s standard library and Unix module provide functions similar to those in Go for working with the file system. The main differences are in syntax and error handling patterns.