I need to join two IO String
s with a -
in between. Here's what I came up with, which works - what's the right way?
import System.Environment
f :: String -> String -> IO String
f x y = (foldl1 (++)) <$> sequence [(getEnv x),(return "-"),(getEnv y)]
You could here use an applicative style function:
f :: String -> String -> IO String
f x y = withHyp <$> getEnv x <*> getEnv y
where withHyp ex ey = ex ++ '-' : ey
So here we join the two String
s that are then joined with a hypen in the middle through the withHyp
function.
Or for a list of environment variables that we need to fetch, we can use mapM
and perform an intercalate
:
import Data.List(intercalate)
f :: [String] -> IO String
f xs = intercalate "-" <$> mapM getEnv xs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With