Skip to content

Instantly share code, notes, and snippets.

@clementi
Last active August 29, 2015 14:20
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 clementi/2a05c7ce7c895c395bb8 to your computer and use it in GitHub Desktop.
Save clementi/2a05c7ce7c895c395bb8 to your computer and use it in GitHub Desktop.
"Which" in Haskell
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