I have a good understanding of imperative programming, but now I am learning myself as Haskell for the great good.
I think I have a good theoretical understanding of Monads, Functors and Applications, but I need practice. And for practice, I sometimes bring some bits from my current work tasks.
And I get a little fixated on combining material with an applicative way.
First question
I have two functions to check:
import Prelude hiding (even) even :: Integer -> Maybe Integer even x = if rem x 2 == 0 then Just x else Nothing isSmall :: Integer -> Maybe Integer isSmall x = if x < 10 then Just x else Nothing
Now I want validate :: Integer -> Maybe Integer built from even and isSmall
My best solution is
validate a = isSmall a *> even a *> Just a
And it does not indicate free
I can use a monad
validate x = do even x isSmall x return x
But why use Monad if (I suppose) all I need is applicative? (And he is still not accurate)
Is this a better (and better) way to do this?
Second question
Now I have two validators with different signatures:
even = ... greater :: (Integer, Integer) -> Maybe (Integer, Integer) -- tuple second element should be greater than the first greater (a, b) = if a >= b then Nothing else Just (a, b)
I need validate :: (Integer, Integer) -> Maybe (Integer, Integer) , which tries greater on the input tuple, and then even on the second element of the set.
And validate' :: (Integer, Integer) -> Maybe Integer with the same logic, but returning the second code element.
validate (a, b) = greater (a, b) *> even b *> Just (a, b) validate' (a, b) = greater (a, b) *> even b *> Just b
But I believe that the input tuple "flows" to greater , and then "flows" to some kind of snd and even , and then only the final element ends in the final Just .
What would a haskeller do?