Execute multiple statements based on boolean conditions in Haskell

596 Views Asked by At

I am new to Haskell I am trying to execute multiple statements if the several boolean values are true. I tried it using guards but it only executes the first statement that is true and it stops. I want it to execute all of them that are true, for example f 5 executing g x and then also executing h x. I tried it using if statements but I couldn't.

f x

  | x < 10 = g x

  | x < 15 = h x

  | otherwise = m x

This is just a simplification, I actually have a different program. The cases are different, I am just trying to have multiple if statements like in other programming languages. So in this example, if x < 10 then do something, ALSO if x < 15 also do this.

2

There are 2 best solutions below

0
On BEST ANSWER

In pure functional programming, we do not execute statement as we do in imperative programming. there is no notion of "do this, then do that". Instead, we mainly write functions that take a few arguments as input, and return some output.

What if we do want to interact with the world, instead of just computing a return value? Well, in this specific case, we resort to the IO monad. Note that this is not something that we do frequently -- we prefer to avoid the IO monad as much as possible, so to keep most of the computation pure.

Here's an example:

f :: Int -> IO ()
f x = do
   putStrLn "hello"
   if x > 15
   then putStrLn "> 15"
   else putStrLn "not > 15"
   if x > 10
   then putStrLn "> 10"
   else putStrLn "not > 10"

If you are a beginner, I would recommend you learn the Haskell basics before using the IO monad. Using the IO monad everywhere would lead to very unidiomatic code.

Most imperative programmers learning FP try to shoehorn their previous habits into FP languages -- it's very common. However, programming with side-effects and mutable variables does not cope well with FP. One gradually need to stop thinking in terms of effectful statements, and instead rewire one's brain so to think in terms of values and (often recursive) definitions.

Note that FP is not special in this aspect. If one learned pure FP first, say in Haskell, and then approached any imperative programming language, they would probably ask thinks like "how do I use a state monad?". Even if you could use a state monad in -say- Java, that would be silly, since using mutable variables would be far more natural. Again, the programming is trying to shoehorn their previous methods into the new language/paradigm, ignoring the new features, and producing unidiomatic code.

0
On

"for example f 5 executing g x and then also executing h x"

And returning what? We have to return some value, that's how Haskell's e-valu-ation goes.

If you want both of the statements' values, you can combine them by putting them in a list:

f x = [ g x | x < 10 ] ++ [ h x | x < 15 ] ++ [ m x | True ]

This list will contain all the results for which the expression to the right of their | (list comprehension guards) evaluate to True. ++ adds two lists together into one. True trivially evaluates to True so can be omitted: [ m x ].