Created
August 30, 2015 17:58
-
-
Save frms-/196644cb41a9a968d6ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import System.Environment (getArgs) | |
import System.FilePath | |
import Data.Maybe (isJust) | |
main :: IO () | |
main = do [from, to] <- getArgs | |
print (relative from to) | |
relative :: FilePath -> FilePath -> FilePath | |
relative from to = case removePrefix (splitDirectories from) (splitDirectories to) of | |
(Nothing:_, Nothing:_) -> "." | |
(xs, ys) -> joinPath $ [".."| _ <- takeWhile isJust xs] ++ [y | Just y <- takeWhile isJust ys] | |
removePrefix :: [FilePath] -> [FilePath] -> ([Maybe FilePath], [Maybe FilePath]) | |
removePrefix xs ys = | |
unzip $ dropWhile (\z -> case z of {(Nothing, Nothing) -> False; (x, y) -> x == y }) $ zip (stream xs) (stream ys) | |
where stream zs = fmap Just zs ++ repeat Nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment