Sister dir path in Haskell

Find this useful? Support us: Star on GitHub 6
Category: File | Language: Haskell

In Haskell, you can get the sister directory path using the directory library. The directory library provides the getDirectoryContents function, which returns a list of files and directories in a given directory.

To get the sister directory path, you can use the following steps:

1. Get the parent directory path using the takeDirectory function from the System.File.Path module.
2. Get the list of directories in the parent directory using the getDirectoryContents function.
3. Filter the list to exclude the current directory (.) and the parent directory (..).
4. Find the index of the current directory in the filtered list using the elemIndex function.
5. Get the directory at the previous index in the list, which is the sister directory.

Here's an example code snippet:

import System.FilePath
import System.Directory
import Data.List (elemIndex)

getSisterDirPath :: FilePath -> IO (Maybe FilePath)
getSisterDirPath dir = do
let parentDir = takeDirectory dir
dirs <- filterM doesDirectoryExist =<< getDirectoryContents parentDir
let filteredDirs = filter (notElem [".", ".."]) dirs
let currDirIndex = elemIndex (takeFileName dir) filteredDirs
case currDirIndex of
Just i -> case drop i filteredDirs of
(sisterDir:_) -> return $ Just $ parentDir sisterDir
[] -> return Nothing
Nothing -> return Nothing



This function takes a directory path as input and returns the path of the sister directory, if it exists, or Nothing otherwise.

Here's an example usage of this function:

main :: IO ()
main = do
  sisterDir <- getSisterDirPath "/home/user/documents/example"
  case sisterDir of
    Just dir -> putStrLn $ "Sister directory path: " ++ dir
    Nothing -> putStrLn "Sister directory not found"

In this example, getSisterDirPath is called with the directory path /home/user/documents/example. If the sister directory exists, its path will be printed. Otherwise, an error message will be printed.