使用Haskell获取从绝对文件路径到另一个文件的相对路径

问题描述 投票:-1回答:1

我正在寻找Haskell函数以给出两个绝对路径之间的相对路径。例如,给:

  1. / home / mydir / images / dir1

  2. / home / mydir / docs / dir2

我想要从1到2的相对路径:../../ docs / dir2

我看了filepathdirectory,但没有找到所需的东西。

是否有函数或库可以执行我想要的工作?

haskell directory filepath relative-path absolute-path
1个回答
0
投票

我还没有找到这样的功能。您可以使用filepath实用程序

构建它
import System.FilePath
import Control.Applicative

relativeTo :: FilePath -> FilePath -> FilePath
relativeTo path1 path2 = joinPath . getZipList $ ZipList commonPath <|> ZipList splitPath1
 where
  splitPath1 = splitDirectories . dropDrive $ path1
  splitPath2 = splitDirectories . dropDrive $ path2
  dirToDots x y = if x == y && (x /= "/")
                    then ".." 
                    else ""
  commonPath = takeWhile (/= "") $ zipWith dirToDots splitPath1 splitPath2

main = print $ "/home/mydir/docs/dir2" `relativeTo` "/home/mydir/images/dir1"
© www.soinside.com 2019 - 2024. All rights reserved.