Haskellでwhichコマンド

基本機能だけです。

import qualified Control.Monad as CM
import qualified Data.Maybe as DM
import System.Directory (doesFileExist)
import System.Environment (getArgs)
import System.FilePath (getSearchPath, (</>))

findExe :: String -> IO (Maybe String)
findExe n = DM.listToMaybe `CM.fmap` (CM.filterM pred =<< getSearchPath)
    where
    pred p = doesFileExist $ p </> n

main :: IO ()
main = do
    ps <- getArgs
    CM.unless (null ps) $ findExe (head ps) >>= DM.maybe (return ()) putStrLn