Skip to content

Instantly share code, notes, and snippets.

@frms-
Created August 30, 2015 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frms-/196644cb41a9a968d6ed to your computer and use it in GitHub Desktop.
Save frms-/196644cb41a9a968d6ed to your computer and use it in GitHub Desktop.
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