1

How to avoid boilerplate assignments like doesExist <- doesDirectoryExist path and case doesExist of ... within IO?

Is there a more idiomatic way than this?

import System.Directory import System.Environment main = do path:_ <- getArgs doesDirectoryExist path >>= cond (putStrLn $ path ++ " Exists") (putStrLn $ path ++ " Does not exist") cond b c a = if a then b else c 
1
  • 2
    BTW, cond is flip bool if you import Data.Bool. Commented Feb 14, 2015 at 13:13

2 Answers 2

2

LambdaCase is applicable here:

{-# LANGUAGE LambdaCase #-} import System.Directory import System.Environment main = do path:_ <- getArgs doesDirectoryExist path >>= \case True -> putStrLn $ path ++ " Exists" _ -> putStrLn $ path ++ " Does not exist" 
Sign up to request clarification or add additional context in comments.

Comments

0

Or same with "if":

doesDirectoryExist path >>= \x -> if x then (putStrLn "Exists") else putStrLn ("Does not") 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.