Last active
August 29, 2015 14:20
-
-
Save clementi/2a05c7ce7c895c395bb8 to your computer and use it in GitHub Desktop.
"Which" in Haskell
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.Directory | |
import System.Environment | |
import System.Info | |
firstJust :: [Maybe a] -> Maybe a | |
firstJust [] = Nothing | |
firstJust (x:xs) = case x of | |
Nothing -> firstJust xs | |
Just y -> Just y | |
main :: IO () | |
main = do | |
args <- getArgs | |
path <- getEnv "PATH" | |
let search = head args | |
auxExtensions = if os == "mingw32" | |
then ["", ".cmd", ".bat"] | |
else [""] | |
exes <- mapM findExecutable $ map (search++) auxExtensions | |
let exe = firstJust exes | |
progName <- getProgName | |
case exe of | |
Nothing -> putStrLn (progName ++ ": no " ++ search ++ " in (" ++ path ++ ")") | |
Just path -> putStrLn path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment