Update
There is an extra limit of this question,
which is to avoid IO
as much as possible.
This limit was originally placed at the end of my question, which seems to be hard to be noticed.
I actually know how to achieve my goal in Haskell, just as I know how to do it in other imperative programming languages - the imperative way.
But I am not using any other imperative programming, right? I am using Haskell. I'd like a Haskell way, a pure way.
I have reorganized my question by relocating the extra limit at a relatively conspicuous place. That was my bad. Lots of thanks to those quick responses.
Original Question
main :: IO ()
main =
putStrLn =<< cachedOrFetched
<$> getLine
<*> getLine
cachedOrFetched :: String -> String -> String
cachedOrFetched cached fetched =
if not $ null cached
then cached
else fetched
The above code performs IO for twice. But the desired behavior is to skip the second IO when the result of the first IO is not null.
I know I can achieve this by using do
or when
.
Given using too many do
s violates my original intention using Haskell,
I probably gonna live with when
.
Or is there a better way? A purer way?
Here is the entire thing
I started learning Haskell about two weeks ago. I am not expecting a job from it, but simply attracted by programming language itself. Because it's the "purest" as far as I know.
At first, everything seemed as good as I expected.
But then I found I have to write IO
s among my pure code.
I spent quite a long time to find a way to do impure-taint-control.
Applicative Functor seemed to be the rescue.
With it, I can "curry" the impure IOs into my pure functions,
which saves a lot of do
, <-
and explicit IO
notations.
However, I got stuck with this very problem -
I am not able to skip unnecessary IOs in pure functions.
Again, a lot of searches and read. Unfortunately, no satisfying answer till now.