Reputation: 2724
I am trying to replicate the following requirements/code in Haskell
Pseudo-Code (the actual evaluations are much more complicated):
if (x == 1)
doX()
else if (y == 1)
doY()
else if (z == 1)
doZ()
else
doSomethingElse()
Right now I have this as WHEN statements for each, but that doesn't give me an ELSE, so I have
when (x == 1) $ do doX
when (y == 1) $ do doY
when (z == 1) $ do doZ
But then how do I manage my else?
Upvotes: 2
Views: 999
Reputation: 54058
This looks like a good option for guards or a case statement. You could do something like
myAction :: Monad m => Int -> Int -> Int -> m ()
myAction x y z
| x == 1 = doX
| y == 1 = doY
| z == 1 = doZ
| otherwise = doSomethingElse
Or you can use the MultiWayIf
extension:
myAction :: Monad m => m ()
myAction = do
x <- getX
y <- getY
z <- getZ
if | x == 1 -> doX
| y == 1 -> doY
| z == 1 -> doZ
| otherwise -> doSomethingElse
-- continue doing more stuff in myAction as needed
Upvotes: 5