How to resolve the algorithm Make directory path step by step in the OCaml programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Make directory path step by step in the OCaml programming language

Table of Contents

Problem Statement

Create a directory and any missing parents. This task is named after the posix mkdir -p command, and several libraries which implement the same behavior. Please implement a function of a single path string (for example ./path/to/dir) which has the above side-effect. If the directory already exists, return successfully. Ideally implementations will work equally well cross-platform (on windows, linux, and OS X). It's likely that your language implements such a function as part of its standard library. If so, please also show how such a function would be implemented.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Make directory path step by step in the OCaml programming language

Source code in the ocaml programming language

let rec mkdir_p path perm =
 if path <> "" then
    try Unix.mkdir path perm with
    | Unix.Unix_error (EEXIST, _, _) when Sys.is_directory path -> ()
    | Unix.Unix_error (ENOENT, _, _) ->
      mkdir_p (Filename.dirname path) perm;
      Unix.mkdir path perm


  

You may also check:How to resolve the algorithm Equilibrium index step by step in the Aime programming language
You may also check:How to resolve the algorithm Sorting algorithms/Counting sort step by step in the Objeck programming language
You may also check:How to resolve the algorithm Esthetic numbers step by step in the Factor programming language
You may also check:How to resolve the algorithm Program termination step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Bioinformatics/base count step by step in the Go programming language