我正在寻找Haskell函数以给出两个绝对路径之间的相对路径。例如,给:
/ home / mydir / images / dir1
/ home / mydir / docs / dir2
我想要从1到2的相对路径:../../ docs / dir2
我看了filepath和directory,但没有找到所需的东西。
是否有函数或库可以执行我想要的工作?
我还没有找到这样的功能。您可以使用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"