Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: Conditionally execute external process with Maybe FilePath

Tags:

haskell

I am struggling to understand a block of code which is extremely easy in imperative world. That's what I need to do: given an executable full path, which is a Maybe FilePath type, I need to execute it conditionally. If the path is a Nothing - print an error, if the path is Just Path - execute it and print message that the file has been executed. Only "Hello, World" can be easier,right? But in Haskell I dug my self into numerous layers of Maybe's and IO's and got stuck. Two concrete questions arise from here: How do I feed a Maybe FilePath into a system or rawSystem? liftM does not work for me here. What is the correct way of doing this kind of conditional branching? Thanks.

like image 269
r.sendecky Avatar asked Mar 03 '26 10:03

r.sendecky


1 Answers

Simple pattern matching will do the job nicely.

case command of
  Just path -> system path >> putStrLn "Done"
  Nothing   -> putStrLn "None specified"
like image 157
hammar Avatar answered Mar 05 '26 07:03

hammar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!